第3天PopupWindow弹出菜单

PopupWindow

显示方法 显示位置
showAsDropDown(View anchor, int xoff, int yoff) 显示在anchor控件的下方
showAtLocation(View parent, int gravity, int x, int y) 显示在parent控件的某个位置

一.PopupWindow介绍

PopupWindow弹出窗体可以在任意位置弹出窗体,而对话框只能出现屏幕最中间。

二.如何自定义窗体

(1)构造方法:public PopupWindow (Context context):context上下文对象
(2)必须设置的3大要素:
setContentView():设置自定义布局
setWidth():设置宽度
setHeight():设置高度
(3)显示窗体:
a。显示在某个指定控件的下方
showAsDropDown(View anchor):
showAsDropDown(View anchor, int xoff, int yoff);//xoff和yoff都是偏移量
b。指定父视图,显示在父控件的某个位置(Gravity.TOP,Gravity.RIGHT等)
showAtLocation(View parent, int gravity, int x, int y);
//gravity可以是Gravity.TOP、Gravity.BOTTOM、Gravity.LEFT、Gravity.RIGHT

三.实现微信QQ支付宝右上角加号弹出窗体

在这里插入图片描述

1.步骤流程:

步骤1.实例化PopupWindow对象
步骤2.设置自定义布局、宽度和高度
步骤3.指定位置显示: showAsDropDown() showAtLocation()

2代码:
(1)xml布局文件:activity_main2.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"
    tools:context=".Main2Activity"
    android:orientation="vertical"
    >
    <RelativeLayout
        android:background="#393A3F"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        >
        <TextView
            android:gravity="center_vertical"
            android:textColor="#fff"
            android:textSize="30sp"
            android:text="微信"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />
        <ImageView
            android:layout_alignParentRight="true"
            android:layout_marginRight="50dp"
            android:src="@drawable/search"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />
        <ImageView
            android:id="@+id/add"
            android:layout_alignParentRight="true"
            android:src="@drawable/add"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />
    </RelativeLayout>

   <ListView
       android:id="@+id/lv"
       android:layout_weight="8"
       android:layout_width="match_parent"
       android:layout_height="0dp"></ListView>

   <RadioGroup
       android:layout_weight="1"
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:orientation="horizontal">
       <RadioButton
           android:textAlignment="center"
           android:drawableTop="@drawable/selector1"
           android:button="@null"
           android:textSize="20sp"
           android:text="微信"
           android:textColor="@drawable/selector2"
           android:layout_weight="1"
           android:layout_width="0dp"
           android:layout_height="match_parent" />
       <RadioButton
           android:textAlignment="center"
           android:drawableTop="@drawable/selector1"
           android:button="@null"
           android:textSize="20sp"
           android:text="微信"
           android:textColor="@drawable/selector2"
           android:layout_weight="1"
           android:layout_width="0dp"
           android:layout_height="match_parent" />
       <RadioButton
           android:textAlignment="center"
           android:drawableTop="@drawable/selector1"
           android:button="@null"
           android:textSize="20sp"
           android:text="微信"
           android:textColor="@drawable/selector2"
           android:layout_weight="1"
           android:layout_width="0dp"
           android:layout_height="match_parent" />
       <RadioButton
           android:textAlignment="center"
           android:drawableTop="@drawable/selector1"
           android:button="@null"
           android:textSize="20sp"
           android:text="微信"
           android:textColor="@drawable/selector2"
           android:layout_weight="1"
           android:layout_width="0dp"
           android:layout_height="match_parent" />
   </RadioGroup>
</LinearLayout>

(2)自定义弹出窗体布局xml文件:layout_weixin_popupwindow.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="#45494A"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:src="@drawable/chat"
            android:layout_weight="3"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
        <TextView
            android:textColor="#fff"
            android:textSize="20sp"
            android:text="发起群聊"
            android:layout_weight="7"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:background="#fff">
        
    </View>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:src="@drawable/chat"
            android:layout_weight="3"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
        <TextView
            android:textColor="#fff"
            android:textSize="20sp"
            android:text="发起群聊"
            android:layout_weight="7"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:background="#fff">
    </View>
</LinearLayout>

(3)Java代码:Main2Activity.java

public class Main2Activity extends AppCompatActivity {
    private ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //TODO 去除自带的bar
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main2);
        imageView = (ImageView) findViewById(R.id.add);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                show_popupwindow();
            }
        });
    }
    //弹出窗体
    public void show_popupwindow(){
        //TODO 1:实例化对象
        PopupWindow popupWindow = new PopupWindow(Main2Activity.this);
        //TODO 2:设置属性
        View view= LayoutInflater.from(this).inflate(R.layout.layout_weixin_popupwindow,null);
        popupWindow.setContentView(view);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setWidth(600);
        //设置点击外部消失
        popupWindow.setOutsideTouchable(true);
        //TODO 3:展示
        popupWindow.showAsDropDown(imageView,-100,-100);
    }

}

四.底部弹出窗体

主要应用于抖音点击分享,打字弹出键盘等
在这里插入图片描述
(1)xml布局文件:activity_main.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"
    tools:context=".MainActivity"
    android:gravity="center">
    <Button
        android:onClick="click"
        android:text="弹出窗体"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

(2)弹出窗体xml布局文件:layout3.xml

<?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="wrap_content"
    android:background="#F0E8F0"
    android:orientation="vertical">
    <TextView
        android:id="@+id/camera_tv"
    android:textAlignment="center"
    android:textSize="30sp"
    android:text="拍照"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
    <View
        android:background="#969696"
        android:layout_width="match_parent"
        android:layout_height="5dp"></View>

    <TextView
        android:textAlignment="center"
        android:textSize="30sp"
        android:text="从手机相册选择"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <View
        android:background="#969696"
        android:layout_width="match_parent"
        android:layout_height="5dp"></View>
    <TextView
        android:textAlignment="center"
        android:textSize="30sp"
        android:text="保存图片"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <View
        android:background="#969696"
        android:layout_width="match_parent"
        android:layout_height="15dp"></View>
    <TextView
        android:textAlignment="center"
        android:textSize="30sp"
        android:text="取消"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

(3)java 代码:MainActivity.java

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void click(View view) {
        show_popupwindow();
    }
    //弹出窗体
    public void show_popupwindow(){
        //TODO 1:实例化对象
        PopupWindow popupWindow = new PopupWindow(MainActivity.this);
        //TODO 2:设置属性
        View view=LayoutInflater.from(this).inflate(R.layout.layout3,null);
        final TextView textView = view.findViewById(R.id.camera_tv);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, textView.getText().toString().trim(), Toast.LENGTH_SHORT).show();
            }
        });
	popupWindow.setContentView(view);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置点击外部消失
        popupWindow.setOutsideTouchable(true);
      	//TODO 3:展示
        /**
         * @param parent 父布局
         * @param gravity gravity可以是Gravity.TOP、Gravity.BOTTOM、Gravity.LEFT、Gravity.RIGHT。。。。
         * @param x x轴偏移量
         * @param y y轴偏移量
         */
        View parent=LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main,null);
        popupWindow.showAtLocation(parent, Gravity.BOTTOM,0,0);
    }

}

猜你喜欢

转载自blog.csdn.net/qq_34178710/article/details/84993160