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"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/Rl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="微信"
android:layout_centerInParent="true"
android:gravity="center"
android:textSize="20sp"
android:textColor="#FFFFFF"/>
<ImageView
android:id="@+id/Iv"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/add"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<ListView
android:id="@+id/Lv"
android:layout_below="@id/Rl"
android:layout_above="@id/Rg"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
<RadioGroup
android:id="@+id/Rg"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#EEEEEE">
<RadioButton
android:id="@+id/Rb1"
android:checked="true"
android:layout_weight="1"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="消息"
android:textColor="@drawable/text_color"
android:drawableTop="@drawable/message"
android:textSize="20sp"
android:button="@null"/>
<RadioButton
android:id="@+id/Rb2"