Android之PopupWindow弹出对话框
Android之PopupWindow弹出对话框
Android的对话框常用的有两种:PopupWindow和AlertDialog。PopupWindow顾名思义为弹出菜单,不同于AlertDialog对话框,PopupWindow弹出的位置可以很多变化,按照有无偏移分,可以分为无偏移和偏移两种;按照参照类型不同又可以分为两种:相对某个控件(Anchor锚)的位置和父容器内部的相对位置。具体如下:
函数 | 简介 |
showAsDropDown(View anchor) | 相对某个控件的位置(正左下方),无偏移 |
showAsDropDown(View anchor, int xoff, int yoff) | 相对某个控件的位置,有偏移(正数表示下方右边,负数表示(上方左边)) |
showAtLocation(View parent, int gravity, int x, int y) | 父容器容器相对位置,例如正中央Gravity.CENTER,下方Gravity.BOTTOM等 |
下面是运行程序截图:
程序代码:
布局:main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/layout"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:id="@+id/tv_showText"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:text="@string/hello"
- android:textSize="22px"
- />
- <Button
- android:id="@+id/bt_PopupWindow1"
- android:text="以自己为Anchor,不偏移"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <Button
- android:id="@+id/bt_PopupWindow2"
- android:text="以自己为Anchor,正下方"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <Button
- android:id="@+id/bt_PopupWindow3"
- android:text="以屏幕中心为参照,不偏移(正中间)"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <Button
- android:id="@+id/bt_PopupWindow4"
- android:text="以屏幕下方为参照,下方中间"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
|
评论暂时关闭