daniu111 发表于 2012-12-19 23:08:38

Android-PopupWindow弹出对话框

Android-PopupWindow弹出对话框

<div class="postbody"><div id="cnblogs_post_body">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等下面是运行程序截图:
http://images.cnblogs.com/cnblogs_com/daniu233/201210/201210222126136540.png
http://images.cnblogs.com/cnblogs_com/daniu233/201210/201210222127279794.png
http://images.cnblogs.com/cnblogs_com/daniu233/201210/201210222128039028.png
http://images.cnblogs.com/cnblogs_com/daniu233/201210/201210222128461091.png
程序代码:
布局文件:main.xml
<div class="cnblogs_code" style="background-color: #f5f5f5; width: 558px; height: 726px; border: #cccccc 1px solid; padding: 5px;"><LinearLayout 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"   android:orientation="vertical"   android:id="@+id/layout">      <TextView android:id="@+id/text_view"      android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:gravity="center"      android:text="@string/hello"      android:textSize="22dp"/>      <Button android:id="@+id/button_test1"      android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:text="以自己为Anchor,不偏移"/>    <Button android:id="@+id/button_test2"      android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:text="以自己为Anchor,正下方"/>      <Button android:id="@+id/button_test3"      android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:text="以屏幕中心为参照,不偏移(正中间)"/>      <Button android:id="@+id/button_test4"      android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:text="以屏幕下方为参照,下方中间"/></LinearLayout>
页: [1]
查看完整版本: Android-PopupWindow弹出对话框