歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android PopUp window的使用

Android PopUp window的使用

日期:2017/3/1 10:16:48   编辑:Linux編程

Android的popupWindow類似一個不能動的widget,它顯示在別的View之上。

具體操作如下:

主View:

/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/main"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:gravity="center"

android:background="@color/background"

>

</LinearLayout>

要顯示在PopupWindow上的View:

<?xml version="1.0" encoding="utf-8"?>


/layout/extra.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent" android:layout_height="wrap_content"

android:orientation="vertical">

<ImageButton android:id="@+id/cancel" android:layout_width="wrap_content"

android:layout_height="wrap_content" android:layout_gravity="top|right"

android:src="@drawable/del" android:layout_marginTop="10dip"

android:layout_marginRight="10dip" android:layout_marginBottom="10dip"

android:background="#0000003D"/>

<ScrollView android:layout_width="wrap_content"

android:layout_height="wrap_content">

<TextView android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="18dip"

android:text="歡迎使用AudioPlayer!\n\n

android:textColor="#99FFFFFF"

android:autoLink="email|web"/>

</ScrollView>

</LinearLayout>

代碼實現:

View extralView = getLayoutInflater().Inflater(R.layout.extra, null);

PopupWindow extral = new PopupWindow(extralView);

接下來就是如何將這個extral觸發顯示出來,通常是用Button觸發,但是也可以通過別的方式:

在onCreate中加入

Looper.myQueue().addIdleHandler(new IdleHandler() {


public boolean queueIdle() {

// TODO Auto-generated method stub

if (extraWindow != null) {

extraWindow.showAtLocation(findViewById(R.id.main), Gravity.TOP,0, 0);

extraWindow.update(0, 25, ScreenWidth, 60);

}

return false;

}

});

ScreenWidth可以通過Diaplay得到。

在這裡的IdleHandler是在後台處理消息的一種方式,當目前用戶沒有操作時觸發。一般,當我們不需要人為觸發時可以通過這種方式觸發。

Copyright © Linux教程網 All Rights Reserved