安卓 PopupWindow 弹出式窗口

PopupWindow 弹出式窗口,在安卓中应用广泛,相比于 PopupMenu 它更灵活,它的位置是可以自己调整的。

先看效果图,点击圆形头像后会在底部弹出一个窗口,每个窗口中子项都可以作为一个点击事件,执行相应的功能。

每一个 PopupWindow 窗口就是一个布局,所以需要一个布局文件,然后去实现它的点击事件。

1. 编写主界面的布局,activity_main.xml ,实现一个背景和圆形头像(这里的圆形头像使用了开源库roundedimageview,需要自己添加)添加地址:https://blog.csdn.net/weixin_43851639/article/details/90545273

<?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=".MainActivity">

    <RelativeLayout
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:background="@drawable/girl2">

        <com.makeramen.roundedimageview.RoundedImageView xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/head"
            android:layout_width="200dp"
            android:layout_height="100dp"
            android:layout_marginTop="30dp"
            android:layout_centerHorizontal="true"
            android:src="@drawable/head"
            app:riv_border_color="#333333"
            app:riv_border_width="1dp"
            app:riv_oval="true" />

    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/bt1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="收藏"/>

    </LinearLayout>


</LinearLayout>

2. 在 layout 目录下新建一个布局文件,命名为 popupwindow。在里面定义了三个按钮。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:layout_alignParentBottom="true">

    <Button
        android:id="@+id/take_photo"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="拍照" />

    <Button
        android:id="@+id/choose_picture"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="相册"/>

    <Button
        android:id="@+id/exit1"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="取消"/>

</LinearLayout>

3. 在 MainActivity.java 中实现点击事件,即点击头像,弹出窗口,然后获取每一个窗口子项的点击事件。

public class MainActivity extends AppCompatActivity {
    
    private ImageView imageView;
    private PopupWindow popupWindow = null; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获取图片id,然后设置监听
        imageView = (ImageView) findViewById(R.id.head);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupWindow();
            }
        });
    }

    //弹窗
    private void showPopupWindow() {
        LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View popupWindowView = inflater.inflate(R.layout.popupwindow,null,false);
        popupWindow = new
                PopupWindow(popupWindowView,ActionBar.LayoutParams.MATCH_PARENT,ActionBar.LayoutParams.WRAP_CONTENT,true);
        //引入依附的布局
        View parentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main,null);
        //相对于父控件的位置
        popupWindow.showAtLocation(parentView,Gravity.BOTTOM,0,0);
        //获取焦点,否则无法点击
        popupWindow.setFocusable(true);

        //按钮点击事件,此处获取按钮的id不同,因为按钮来自popupwindow的视图
        //拍照按钮
        Button takeBtn = popupWindowView.findViewById(R.id.take_photo);
        takeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(),"拍照设置头像",Toast.LENGTH_SHORT).show();
                //调用拍照函数
                popupWindow.dismiss();
                takePhoto();
            }
        });
        //相册按钮
        Button chooseBt = popupWindowView.findViewById(R.id.choose_picture);
        chooseBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(),"相册选择照片",Toast.LENGTH_SHORT).show();
                //调用相册函数
                popupWindow.dismiss();
                choosePicture();
            }
        });
        //取消按钮
        Button exitBt = popupWindowView.findViewById(R.id.exit1);
        exitBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(),"取消",Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
            }
        });
    }

划重点,划重点,划重点,怎么获取弹窗子项的 id,然后监听他们的点击事件。获取按钮id和以往不同,在上面代码中有,这里再写出来。注意是在 popupWindowView 对象中获取。

//定义一个对象 popupWindowView,承载弹窗布局,然后需要在 popupWindowView 中获取子项 id
View popupWindowView = inflater.inflate(R.layout.popupwindow,null,false);
...//省略中间代码

//按钮点击事件,此处获取按钮的id不同,因为按钮来自popupwindow的视图
Button takeBtn = popupWindowView.findViewById(R.id.take_photo);

到这里弹窗的功能已经实现了,而且已经成功监听了弹窗子项,至于每个子项实现的具体功能,要看自己的需求了。

https://blog.csdn.net/weixin_40430041/article/details/79099886

发布了38 篇原创文章 · 获赞 18 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43851639/article/details/90544541
今日推荐