实现PopupWindow居中显示 亦凉 2022-02-19 11:41 640阅读 0赞 //这一步是为了获取屏幕的宽高,便于用来设置PopupWindow的大小 DisplayMetrics dm = getResources().getDisplayMetrics(); /*popupwindow*/ /*新建popupwindow依靠的view*/ view = View.inflate(MainActivity.this,R.layout.pop_layout,null); /*创建pop(视图,宽,高)*/ popupWindow = new PopupWindow(view,dm.widthPixels/11*9,dm.heightPixels/11*9); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE)); //展示pop popupWindow.showAtLocation(MainActivity.this.getWindow().getDecorView(),Gravity.CENTER,0,0); 其中R.layout.pop\_layout的内容是 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="300dp"> <TextView android:layout_margin="40dp" android:text="这是一个pop" android:layout_gravity="center" android:textSize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:text="按键1" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:text="按键2" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
还没有评论,来说两句吧...