Android 中PopupWindow弹出式窗口的使用

简介: Android 中PopupWindow弹出式窗口的使用

效果图如下:

实现代码如下:

activity_popup_window.xml按钮

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".PopupWindowActivity">
    <Button
        android:id="@+id/btn_popupWindow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="PopupWindow" />
</LinearLayout>

自定义弹出的视图layout_pop.xml,也可以用RecycleView或者ListView

<?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:orientation="vertical">
    <TextView
        android:id="@+id/tv_good"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"
        android:text="好"
        android:textColor="@color/gray"
        android:textSize="20sp" />
    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="@color/gray" />
    <TextView
        android:id="@+id/tv_not_too_bad"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"
        android:text="还行"
        android:textColor="@color/gray"
        android:textSize="20sp" />
    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="@color/gray" />
    <TextView
        android:id="@+id/tv_bad"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"
        android:text="不好"
        android:textColor="@color/gray"
        android:textSize="20sp" />
</LinearLayout>

PopupWindowActivity类实现代码如下:

public class PopupWindowActivity extends AppCompatActivity {
    private Button btn_popupWindow;
    private PopupWindow popupWindow;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popup_window);
        btn_popupWindow = findViewById(R.id.btn_popupWindow);
        btn_popupWindow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View popup_view = LayoutInflater.from(PopupWindowActivity.this).inflate(R.layout.layout_pop, null);
                TextView textView = popup_view.findViewById(R.id.tv_good);
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        popupWindow.dismiss();
                        Toast.makeText(PopupWindowActivity.this, "好", Toast.LENGTH_SHORT).show();
                    }
                });
                popupWindow = new PopupWindow(popup_view, btn_popupWindow.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT);
                //设置弹出窗口应该接收外部触摸事件
                popupWindow.setOutsideTouchable(true);
                //设置可聚焦
                popupWindow.setFocusable(true);
                popupWindow.showAsDropDown(btn_popupWindow);
            }
        });
    }
}

以上就是PopupWindow弹出式窗口的简单使用~


目录
相关文章
|
Android开发 索引 容器
【Android 应用开发】Android 组件 位置坐标 属性 ( 组件位置属性 | 父容器坐标系坐标 | 窗口坐标系坐标 | 屏幕坐标系坐标 | 触摸坐标 )
【Android 应用开发】Android 组件 位置坐标 属性 ( 组件位置属性 | 父容器坐标系坐标 | 窗口坐标系坐标 | 屏幕坐标系坐标 | 触摸坐标 )
254 0
|
Android开发 C++
【Android 逆向】IDA 工具使用 ( IDA 32 位 / 64 位 版本 | 汇编代码视图 IDA View-A | 字符串窗口 Strings window )
【Android 逆向】IDA 工具使用 ( IDA 32 位 / 64 位 版本 | 汇编代码视图 IDA View-A | 字符串窗口 Strings window )
849 0
【Android 逆向】IDA 工具使用 ( IDA 32 位 / 64 位 版本 | 汇编代码视图 IDA View-A | 字符串窗口 Strings window )
|
7月前
|
Android开发 开发者
Android播放器实现视频窗口实时放大缩小功能
很多开发者希望Android播放端实现视频窗口的放大缩小功能,为此,我们做了个简单的demo,通过播放端回调RGB数据,直接在上层view操作处理即可,Github:https://github.com/daniulive/SmarterStreaming
190 0
|
Android开发
Android 裁剪摄像头预览窗口-SurfaceView
Android 裁剪摄像头预览窗口-SurfaceView
542 0
Android 裁剪摄像头预览窗口-SurfaceView
|
存储 前端开发 Linux
Android窗口管理分析(4):Android View绘制内存的分配、传递、使用
Android窗口管理分析(4):Android View绘制内存的分配、传递、使用
305 0
|
Android开发 Windows
Android窗口管理分析(3):窗口分组及Z-order的确定
Android窗口管理分析(3):窗口分组及Z-order的确定
438 0
Android窗口管理分析(3):窗口分组及Z-order的确定
|
Android开发 容器
Android窗口管理分析(2):WindowManagerService窗口管理之Window添加流程
Android窗口管理分析(2):WindowManagerService窗口管理之Window添加流程
410 0
Android窗口管理分析(2):WindowManagerService窗口管理之Window添加流程
|
Linux Android开发 开发者
Android窗口管理分析(1):View如何绘制到屏幕上的主观理解
Android窗口管理分析(1):View如何绘制到屏幕上的主观理解
222 0
Android窗口管理分析(1):View如何绘制到屏幕上的主观理解
|
Android开发
从Toast显示原理初窥Android窗口管理
从Toast显示原理初窥Android窗口管理
180 0
从Toast显示原理初窥Android窗口管理
|
API Android开发
Android屏幕窗口各部分超详细介绍
目前,android屏幕上大致分成三个部分:通知栏,内容区、虚拟导航栏(NavigatorBar) 因为这三个区域引起的问题数不胜数,必须好好总结一下
490 0