android PopupWindow
1.构造函数
//方法一: public PopupWindow (Context context) //方法二: public PopupWindow(View contentView) //方法三: public PopupWindow(View contentView, int width, int height) //方法四: public PopupWindow(View contentView, int width, int height, boolean focusable)
2.显示函数
//相对某个控件的位置(正左下方),无偏移 showAsDropDown(View anchor): //相对某个控件的位置,有偏移;xoff表示x轴的偏移,正值表示向左,负值表示向右;yoff表示相对y轴的偏移,正值是向下,负值是向上; showAsDropDown(View anchor, int xoff, int yoff): //相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移 showAtLocation(View parent, int gravity, int x, int y):
3.其他常用函数
public void dismiss() //消失 public void setFocusable(boolean focusable) //获取焦点,点击空白处消失 public void setTouchable(boolean touchable) //设置PopupWindow是否响应touch事件,默认是true public void setOutsideTouchable(boolean touchable) //PopupWindow以外的区域是否可点击,即如果点击PopupWindow以外的区域,PopupWindow是否会消失。
public void setBackgroundDrawable(Drawable background)//只有设置了这个,setOutsideTouchable才有用
全屏显示前提mPopWindow的根目录需要设置
android:layout_width="match_parent"
android:layout_height="match_parent"
使用代码
mPopWindow = new PopupWindow(contentView,
ViewGroup.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
true);
popwinodw显示的时候设置背景透明度
public void backgroundAlpha(float bgAlpha)
{
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = bgAlpha; //0.0-1.0
getWindow().setAttributes(lp);
}
popwinodw show之前设置
backgroundAlpha(0.5f);
这样的话消失的时候背景透明度是不变的,这个显然和我们的想法是相悖的,所以需要设置监听
mPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
backgroundAlpha(1f);
}
});
popwinodw代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="true"
android:fitsSystemWindows="true"
android:gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:background="@drawable/corner5"
android:orientation="vertical">
<LinearLayout
android:id="@+id/computer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_marginLeft="24dp"
android:src="@drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:text="计算机"
android:textColor="#505050"
android:textSize="16sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#d6d6d6" />
<LinearLayout
android:id="@+id/network"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_marginLeft="24dp"
android:src="@drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:text="网络"
android:textColor="#505050"
android:textSize="16sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#d6d6d6" />
<LinearLayout
android:id="@+id/popWindowDismiss"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:text="取消"
android:textColor="#d6d6d6"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
居中显示PopWindow效果图

代码
private void showPopupWindow() {
//设置contentView
View contentView = LayoutInflater.from(CustomActivity.this).inflate(R.layout.popwindow_change, null);
mPopWindow = new PopupWindow(contentView,
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
// mPopWindow = new PopupWindow(contentView,
// ViewGroup.LayoutParams.MATCH_PARENT,
// WindowManager.LayoutParams.MATCH_PARENT,
// true);
mPopWindow.setFocusable(true);
mPopWindow.setBackgroundDrawable(new BitmapDrawable());
backgroundAlpha(0.5f);
mPopWindow.setContentView(contentView);
//设置各个控件的点击响应
LinearLayout tv1 = (LinearLayout)contentView.findViewById(R.id.computer);
LinearLayout tv2 = (LinearLayout)contentView.findViewById(R.id.network);
LinearLayout tv3 = (LinearLayout)contentView.findViewById(R.id.popWindowDismiss);
tv1.setOnClickListener(this);
tv2.setOnClickListener(this);
tv3.setOnClickListener(this);
//显示PopupWindow rootview是当前Activity的view
View rootview = LayoutInflater.from(CustomActivity.this).inflate(R.layout.activity_custom, null);
mPopWindow.showAtLocation(rootview, Gravity.CENTER, 0, 0);
mPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
backgroundAlpha(1f);
}
});
}
从底部显示效果图:

代码:
剧中显示代码改一句话
mPopWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);
浙公网安备 33010602011771号