PopupWindow的使用簡介

1、簡介
java

PopupWindow通常用於View控件周邊的彈出窗口。android


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

建立一個新的彈出窗口能夠顯示contentView。窗戶的尺寸必須傳遞給構造函數。ide

2、使用
函數

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對象spa

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

-------------------------------------------------------------------------------------------------------------------------------
code

4.設置PopupWindow的動畫風格(可選)xml

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正下彈出(左邊緣對齊)

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

相關文章
相關標籤/搜索