一个用于显示信息的UI控件,类似dialog
PopupWindow有9个构造方法,列举4个常用的
View inflate = LayoutInflater.from(this).inflate(R.layout.layout_popup_window, null);
new PopupWindow();
new PopupWindow(inflate);//布局
new PopupWindow(inflate,100,200);//布局,宽,高
new PopupWindow(inflate,100,200,true);//布局,宽,高,是否可聚焦
核心:PopupWindow 必须设置 view,宽,高,三个属性,构造方法中没有设置就必须代码设置
下面 等同
View inflate = LayoutInflater.from(this).inflate(R.layout.layout_popup_window, null);
PopupWindow popupWindow = new PopupWindow();
popupWindow.setContentView(inflate);
popupWindow.setWidth(200);
popupWindow.setHeight(400);
popupWindow.setFocusable(true);
----------------------------------------------------------
PopupWindow popupWindow1 = new PopupWindow(inflate);
popupWindow1.setWidth(200);
popupWindow1.setHeight(400);
popupWindow1.setFocusable(true);
----------------------------------------------------------
PopupWindow popupWindow2 = new PopupWindow(inflate, 100, 200);
popupWindow2.setFocusable(true);
----------------------------------------------------------
PopupWindow popupWindow3 = new PopupWindow(inflate, 100, 200, true);
其他属性:
setContentView(View contentView):设置PopupWindow显示的View
getContentView():获得PopupWindow显示的View
重点 popupWindow1.setFocusable(true);//点击外部消失,这不是这个方法的重点,重点是:
我将他理解为 重点在于拦截事件
- 设置为true,点击外部消失的同时,会拦截这次的点击事件下发,
- 设置为false,点击外部,外部不会消失,并且不会拦截事件,外部会继续响应这次点击事件。
例子:设置为true,这时按返回按钮会将popup直接消失,而不会退出app或者当前页面
popupWindow.setOutsideTouchable(true);//点击外部消失,这才是这个功能的api
popupWindow.setBackgroundDrawable();//设置背景
popupWindow.setAnimationStyle(R.anim.ss);//设置动画
popupWindow.setOnDismissListener(() -> {});//设置popup消失的监听,popup消失的时候回调
显示 popup有三种方法:
- popupWindow.showAsDropDown(tv);显示在tv的正下方
- popupWindow.showAsDropDown(tv,100,100);显示在tv的正下方 ,偏移100,100的地方
- popupWindow.showAsDropDown(tv,20,0, Gravity.RIGHT);//tv的右边和popup对齐,tv的右下角为起点,哈我这么理解
popupWindow.showAtLocation(tv, Gravity.CENTER, 100, 0);//相对于父控件,tv所在的容器
popupWindow显示的时候 外部背景变成透明灰色,改变activity的透明度,或者 popup的布局全屏,然后四周手动设置成透明背景
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha =1f;
getWindow().setAttributes(lp);
栗子:
public class MainActivity extends AppCompatActivity {
private TextView tv;
private PopupWindow popupWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = findViewById(R.id.tv);
showPopupW();
tv.setOnClickListener(v ->{
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha =1f;
getWindow().setAttributes(lp);
popupWindow.showAsDropDown(tv);
});
}
private void showPopupW() {
View inflate = LayoutInflater.from(this).inflate(R.layout.layout_popup_window, null);
TextView tv1 = inflate.findViewById(R.id.tv1);
TextView tv2 = inflate.findViewById(R.id.tv2);
tv2.setOnClickListener(v ->{
popupWindow.dismiss();
});
popupWindow = new PopupWindow();
popupWindow.setContentView(inflate);
popupWindow.setWidth(200);
popupWindow.setHeight(400);
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_launcher_foreground));
popupWindow.setAnimationStyle(R.anim.ss);
popupWindow.setOnDismissListener(() -> {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha =1f;
getWindow().setAttributes(lp);
});
}
}
layout_popup_window.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="200dp"
android:orientation="vertical"
android:layout_gravity="center"
android:background="@color/colorAccent"
android:gravity="center">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是第一个"
android:textSize="12sp"
android:textStyle="bold"
android:textColor="#000000"
android:gravity="center"/>
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是第一个"
android:textSize="12sp"
android:textStyle="bold"
android:textColor="#000000"
android:gravity="center"/>
</LinearLayout>
ss.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="1"
android:toAlpha="0"
android:duration="5000">
</alpha>
</set>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>