Android中还是会经常用到Popupwindow,一种类似于对话框风格的窗口,当然类似于对话框风格也可以用Activity,可以参考:Android中使用Dialog风格弹出框的Activity
一般使用Popupwindow创建对话框风格的窗口只需要两部:
(1)调用Popupwindow的构造函数创建Popupwindow对象,例如
PopupWindow popupWindow = new PopupWindow(root, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
(2)调用Popupwindow的showAsDropDown(View v)将Popupwindow作为v组件的下拉组件显示出来;或者调用Popupwindow的showAtLocation()方法将Popupwindow在指定位置显示。两者可以一起设置
下面就要开始动手啦
首先在activity_main.xml布局文件中添加设置按钮,用于当点击这个按钮时弹出Popupwindow-对话框风格的窗口
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="@dimen/activity_tabhost_height"
android:background="@color/blue">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="探索Popupwindow"
android:textColor="@color/white"
android:textSize="@dimen/acitvity_title_size" />
android:id="@+id/tv_set"
android:layout_width="75dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:gravity="center"
android:text="设置"
android:textColor="@color/white"
android:textSize="@dimen/acitvity_title_size" />
然后再创建一个叫activity_set.xml的布局文件,用于点击设置按钮时加载将显示对话框风格的窗口的XML布局文件
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="65dp"
android:orientation="horiz