public class MainActivity extends AppCompatActivity { private RelativeLayout rl; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rl = (RelativeLayout) findViewById(R.id.rl_root); } public void showPopWindow(View v){ TextView tv = new TextView(this); tv.setText("你好PopupWindow"); //创建popupwindow设置挂载的View控件,宽高,是否可以获得焦点 PopupWindow pop = new PopupWindow(tv,100,100,true);//设置背景图片 如果需要透明背景ColorDrawable创建的时候不需要传参默认就是透明的pop.setBackgroundDrawable(new ColorDrawable(Color.RED)); //现在在那个布局上,居中,xy偏移量 pop.showAtLocation(rl, Gravity.CENTER,0,0); }}
<Button android:layout_width="wrap_content" android:text="弹弹弹" android:onClick="showPopWindow" android:layout_height="wrap_content" />
android PopupWindow的简单实用
最新推荐文章于 2021-06-09 17:14:39 发布