前幾天用到了PopupWindow,在PopupWindow上面放了個ListView,在使用過程中遇到了以下問題:
一、PopupWindow上面的ListView無法點選;
二、PopupWindow上面ListView的高度調整;
三、PopupWindow無法用Back鍵取消。
下面來具體介紹一下PopupWindow的使用:
首先布局PopupWindow:1個ListView
<?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="wrap_content"
android:baselineAligned="false"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/popListLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@+id/father_lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#CCCCCC" >
</ListView>
</LinearLayout>
<LinearLayout
android:id="@+id/popfooterLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#99000000" >
</LinearLayout>
</LinearLayout>
然後初始化PopupWindow View mypopupView = getLayoutInflater().inflate(R.layout.popup_item,null, false);
list = new ArrayList<String>();
for(int i=0;i<10;i++){
list.add("item"+i);
}
fatherLV = (ListView) mypopupView.findViewById(R.id.father_lv);
fatherLV.setAdapter(new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1,list));
fatherLV.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "position"+arg2, 1).show();
}
});;
popView = new PopupWindow(mypopupView, -2, -2);
初始化完畢就可以在需要的地方調用了,不過這樣直接調用的結果是ListView不能點選,應該在初始化的時候加上如下代碼:
View mypopupView = getLayoutInflater().inflate(R.layout.popup_item,null, false);
list = new ArrayList<String>();
for(int i=0;i<10;i++){
list.add("item"+i);
}
fatherLV = (ListView) mypopupView.findViewById(R.id.father_lv);
fatherLV.setAdapter(new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1,list));
fatherLV.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "position"+arg2, 1).show();
}
});;
popView = new PopupWindow(mypopupView, -2, -2);
// 加上這個屬性,PopupWindow的ListView的Item才可以點選
popView.setFocusable(true);
調用及取消
if (popView != null&&popView.isShowing()) {
popView.dismiss();
return;
} else {
initPop();
popView.showAsDropDown(v, 0, 0);
}
使用如上方法調用PopupWindow,再次點選會取消,但是如上設定後,發現還是不能取消,需要給PopupWindow添加OnTouchListener,這樣便可以取消PopupWindow,還可以在點選PopupWindow的空白區域的時候取消PopupWindow
mypopupView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (popView != null && popView.isShowing()) {
popView.dismiss();
popView = null;
}
return false;
}
});
ListView高度問題:
先看效果:
當我們需要下面始終留有一部分空白的時候,就需要調整ListView的高度,不在xml檔案裡面設定具體高度的情況下,我們可以通過如下Java代碼設定,取螢幕一半的高度作為ListView的高度
DisplayMetrics metric = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);
int height = metric.heightPixels; // 螢幕高度(像素)
LinearLayout.LayoutParams popLp = new LayoutParams(-1, height/2);
fatherLV.setLayoutParams(popLp);
Back鍵取消PopupWindow:
這個問題很讓人蛋疼,雖然解決了,但是還是布吉島為什麼這樣做就可以了,一句話解決問題:别看是設定背景,不會對你的背景産生任何的影響哦。
popView.setBackgroundDrawable(new BitmapDrawable());
如上就是我使用popupWindow的時候遇到的幾個問題,希望能幫到大家。