当前位置:首页 > Windows程序 > 正文

PopupWindow的使用简介

2021-03-24 Windows程序

PopupWindow一般用于View控件周边的弹出窗口。


public PopupWindow (View contentView, int width, int height, boolean focusable)

创建一个新的弹出窗口可以显示contentView。窗户的尺寸必须传递给构造函数。

二、使用

1.创建弹出窗口popup_widow.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical" >          <RadioButton          android:paddingLeft="10dp"         android:paddingRight="15dp"         android:layout_width="match_parent"         android:layout_height="0dp"         android:layout_weight="1"         android:text="@string/str_hdmi14"         android:textColor="@color/white"         android:textSize="32sp"          android:button="@drawable/radio_btn"         android:background="@drawable/xxx"/>          <RadioButton         android:paddingLeft="10dp"         android:paddingRight="15dp"         android:layout_width="match_parent"         android:layout_height="0dp"         android:layout_weight="1"         android:text="@string/str_hdmi20"         android:textColor="@color/white"         android:textSize="32sp"          android:button="@drawable/radio_btn"         android:background="@drawable/xxx"/> </LinearLayout>


2.在java代码中用LayoutInflater加载该窗口布局文件

View rootView =  LayoutInflater.from(mContext).inflate( R.layout.popup_window, null);

3.创建PopupWindow对象

 PopupWindow popupWindow = new PopupWindow(rootView,300, 200, true);

-------------------------------------------------------------------------------------------------------------------------------

4.设置PopupWindow的动画风格(可选)

popupWindow.setAnimationStyle(R.style.popup_window_anim);

4.1在values/style.xml定义动画风格

<?xml version="1.0" encoding="utf-8"?> <resources>     <style name="popup_window_anim">         <item name="android:windowEnterAnimation">@anim/popup_window_show</item>         <item name="android:windowExitAnimation">@anim/popup_window_hide</item>     </style> </resources>

4.2 弹出动画 anim/pop_window_show.xml

<?xml version="1.0" encoding="utf-8"?> <set  xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:interpolator="@android:anim/accelerate_decelerate_interpolator"           android:fromXScale="1.0"           android:toXScale="1.0"           android:fromYScale="0.0"           android:toYScale="1.0"           android:fillAfter="false"           android:duration="200"> </scale> </set>

4.3消失动画 anim/pop_window_hide.xml

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android">     <scale android:interpolator="@android:anim/accelerate_decelerate_interpolator"               android:fromXScale="1.0"               android:toXScale="1.0"               android:fromYScale="1.0"               android:toYScale="0.0"               android:fillAfter="false"               android:duration="200">     </scale> </set>

-------------------------------------------------------------------------------------------------------------------------------

5.显示PopupWindow

 popupWindow.showAsDropDown(btn ,xOff ,yOff);

参数:

btn: 在btn这个View控件周边弹出

xOff:x轴的偏移

yOff:y轴的偏移

注:如xOff = 0; yOff=0;则窗口在btn正下弹出(左边缘对齐)

-------------------------------------------------------------------------------------------------------------------------------


PopupWindow的使用简介

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/66766.html