poupwindow的应用+自定义对话框+自定义窗体
一.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
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:text