popupwindow在我们的一些大型手机app上经常出现,例如微信、QQ等。它和Dialog相似,但是位置摆放好像更加灵活。俩者区别详细咨询百度。popupwindow效果如下图,点击加号弹出一个popupwindow显示于屏幕最上方
1.先创建mainActivity的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.lzf.popupwindow.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="74dp"
android:background="@color/colorWeChat"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingTop="24dp">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingEnd="16dp"
android:paddingStart="16dp"
android:text="微信"
android:textColor="@color/colorWhite"
android:textSize="19sp"/>
<ImageView
android:id="@+id/iv_add"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:background="@drawable/selector_bg_img"
android:paddingEnd="16dp"
android:paddingStart="16dp"
android:src="@drawable/icon_add"/>
</RelativeLayout>
</RelativeLayout>
2.在mainActivity代码中对加号按钮进行监听
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView iv_add;
private TextView tv_1, tv_2, tv_3, tv_4, tv_5;
private PopupWindow mPopupWindow;
public static Toast toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// 实现透明状态栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
setContentView(R.layout.activity_main);
init();
}
private void init() {
mPopupWindow = new PopupWindow(this);
iv_add = findViewById(R.id.iv_add);
iv_add.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.iv_add:
showPop();显示popupwindow
break;
case R.id.tv_1:
mPopupWindow.dismiss();
showToast(MainActivity.this, "添加联系人");
break;
case R.id.tv_2:
mPopupWindow.dismiss();
Toast.makeText(this, tv_2.getText(), Toast.LENGTH_SHORT).show();
break;
case R.id.tv_3:
mPopupWindow.dismiss();
Toast.makeText(this, tv_3.getText(), Toast.LENGTH_SHORT).show();
break;
case R.id.tv_4:
mPopupWindow.dismiss();
Toast.makeText(this, tv_4.getText(), Toast.LENGTH_SHORT).show();
break;
case R.id.tv_5:
mPopupWindow.dismiss();
Toast.makeText(this, tv_5.getText(), Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
private void showPop() {
// 设置布局文件
mPopupWindow.setContentView(LayoutInflater.from(this).inflate(R.layout.pop_add, null));
// 为了避免部分机型不显示,我们需要重新设置一下宽高
mPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
// 设置pop透明效果
mPopupWindow.setBackgroundDrawable(new ColorDrawable(0x0000));
// 设置pop出入动画
mPopupWindow.setAnimationStyle(R.style.pop_add);
// 设置pop获取焦点,如果为false点击返回按钮会退出当前Activity,如果pop中有Editor的话,focusable必须要为true
mPopupWindow.setFocusable(true);
// 设置pop可点击,为false点击事件无效,默认为true
mPopupWindow.setTouchable(true);
// 设置点击pop外侧消失,默认为false;在focusable为true时点击外侧始终消失
mPopupWindow.setOutsideTouchable(true);
// 相对于 + 号正下面,同时可以设置偏移量
mPopupWindow.showAsDropDown(iv_add, -100, 0);
// 设置pop关闭监听,用于改变背景透明度
mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
// toggleBright();
}
});
tv_1 = mPopupWindow.getContentView().findViewById(R.id.tv_1);
tv_2 = mPopupWindow.getContentView().findViewById(R.id.tv_2);
tv_3 = mPopupWindow.getContentView().findViewById(R.id.tv_3);
tv_4 = mPopupWindow.getContentView().findViewById(R.id.tv_4);
tv_5 = mPopupWindow.getContentView().findViewById(R.id.tv_5);
tv_1.setOnClickListener(this);
tv_2.setOnClickListener(this);
tv_3.setOnClickListener(this);
tv_4.setOnClickListener(this);
tv_5.setOnClickListener(this);
}
public static void showToast(Context context, String string) {
// TODO Auto-generated method stub
if(toast == null){
// 如果Toast对象为空了,那么需要创建一个新的Toast对象
toast = Toast.makeText(context, string, Toast.LENGTH_LONG);
}else {
// 如果toast对象还存在,那么就不再创建新的Toast对象
toast.setText(string);
}
// 最后调用show方法吐丝
toast.show();
}
}
其中R.layout.pop_add.xml是popupwindow的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/transparent"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:background="@drawable/icon_other_9"
android:orientation="vertical"
android:paddingBottom="20dp"
android:paddingEnd="16dp"
android:paddingStart="26dp"
android:paddingTop="20dp">
<TextView
android:id="@+id/tv_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="发起群聊"
android:textColor="@color/colorWhite"
android:textSize="16sp"/>
<TextView
android:id="@+id/tv_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="添加朋友"
android:textColor="@color/colorWhite"
android:textSize="16sp"/>
<TextView
android:id="@+id/tv_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="扫一扫"
android:textColor="@color/colorWhite"
android:textSize="16sp"/>
<TextView
android:id="@+id/tv_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="收付款"
android:textColor="@color/colorWhite"
android:textSize="16sp"/>
<TextView
android:id="@+id/tv_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="帮助与反馈"
android:textColor="@color/colorWhite"
android:textSize="16sp"/>
</LinearLayout>
</LinearLayout>