Android PopUp window的使用


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是在后台处理消息的一种方式,当目前用户没有操作时触发。一般,当我们不需要人为触发时可以通过这种方式触发。

相关内容