在開(kāi)發(fā)項(xiàng)目中會(huì)經(jīng)常遇到彈窗選擇的情況秀存,這時(shí)就會(huì)用到PopupWindow捶码,PopupWindow的用法比較簡(jiǎn)單,但不熟悉的情況下會(huì)遇到一些問(wèn)題或链,可能會(huì)花不短的時(shí)間來(lái)處理惫恼,這里簡(jiǎn)單總結(jié)一下可能會(huì)遇到的問(wèn)題。見(jiàn)下面代碼澳盐。
PopupWindow
final PopupWindow popupWindow =new PopupWindow(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(LayoutInflater.from(this).inflate(R.layout.popup_window, null));
popupWindow.setBackgroundDrawable(newColorDrawable(ContextCompat.getColor(this,R.color.half)));//設(shè)置背景色祈纯,需要設(shè)置令宿,不設(shè)置可能會(huì)造成返回鍵不起作用
//popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(this,R.mipmap.ic_launcher));//設(shè)置背景圖
popupWindow.setFocusable(false);//物理鍵是否響應(yīng),為true時(shí)腕窥,點(diǎn)返回鍵 ?//popupWindow消失粒没,為false時(shí),點(diǎn)返回鍵activity消失簇爆。popupWindow.setOutsideTouchable(true);//點(diǎn)擊popupWindow外面消失popupWindow.setAnimationStyle(R.style.PopupWindow);//設(shè)置動(dòng)畫(huà)效果findViewById(R.id.popup_window).setOnClickListener(new View.OnClickListener() {
@Override
public voidonClick(View v) {
if(android.os.Build.VERSION.SDK_INT==24) {//在android 7.0中,當(dāng)popupWindow的高度 ?過(guò)大時(shí)癞松,調(diào)用showAsDropDown方法popupWindow可能會(huì)出現(xiàn)在view的上方或占滿全屏,這是android 7.0的bug入蛆,用這種方式可以正常顯示响蓉,7.1已經(jīng)修復(fù)這個(gè)bug
int[] a =new int[2];
v.getLocationInWindow(a);
popupWindow.showAtLocation(getWindow().getDecorView(),Gravity.NO_GRAVITY,0,a[1] + v.getHeight());
}else{
popupWindow.showAsDropDown(v);
}
}
});
代碼中注釋寫(xiě)的比較清楚了,需要注意的是PopupWindow要設(shè)置背景色安寺,以及可以通過(guò)設(shè)置setFocusable來(lái)實(shí)現(xiàn)點(diǎn)擊返回鍵是使PopupWindow消失還是直接finish當(dāng)前界面厕妖,另外還需要注重,在android 7.0系統(tǒng)中挑庶,當(dāng)PopupWindow中的視圖高度過(guò)大時(shí),PopupWindow會(huì)占滿全屏软能,這是系統(tǒng)的bug迎捺,需要做特殊處理,見(jiàn)上面代碼查排。
ListPopupWindow是api 11 之后新加的組件凳枝,在列表選擇時(shí)使用ListPopupWindow十分方便,他的用法和PopupWindow相識(shí)跋核,但增加了適配器adapter岖瑰,用法和listview一樣,如下砂代。
ListPopupWindow
final ListPopupWindow listPopupWindow =new ListPopupWindow(this);
listPopupWindow.setWidth(getResources().getDisplayMetrics().widthPixels);//設(shè)置寬度
listPopupWindow.setHeight(ListPopupWindow.MATCH_PARENT);//設(shè)置高度
listPopupWindow.setBackgroundDrawable(newColorDrawable(ContextCompat.getColor(this,R.color.half)));//設(shè)置背景色
listPopupWindow.setAdapter(newPopupWindowAdapter(this));
listPopupWindow.setAnchorView(findViewById(R.id.popup));
listPopupWindow.setModal(false);//設(shè)置為true響應(yīng)物理鍵listPopupWindow.setHorizontalOffset(100);//垂直間距l(xiāng)istPopupWindow.setVerticalOffset(100);//水平間距findViewById(R.id.popup).setOnClickListener(newView.OnClickListener() {
@Override
public void onClick(View v) {//同樣android 7.0有顯示問(wèn)題蹋订,通過(guò)重置高度可以解決。
if(Build.VERSION.SDK_INT==24) {
int[] a =new int[2];
v.getLocationInWindow(a);
listPopupWindow.setHeight(getResources().getDisplayMetrics().heightPixels- a[1] - v.getHeight());
}
listPopupWindow.show();
}
});
listPopupWindow.setOnItemClickListener(newAdapterView.OnItemClickListener() {//item 點(diǎn)擊事件
@Override
public void onItemClick(AdapterView parent,View view, intposition, longid) {
listPopupWindow.dismiss();
}
});
PopupWindowAdapter
public class PopupWindowAdapter extends BaseAdapter {
private Context context;
public PopupWindowAdapter(Context context) {
this.context= context;
}
@Override
public int getCount() {
return 10;
}
@Override
public Object getItem(intposition) {
return null;
}
@Override
public long getItemId(intposition) {
return0;
}
@Override
public View getView(intposition,View convertView,ViewGroup parent) {
ViewHolder viewHolder;
if(convertView ==null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_popup, null);
}else{
}
return convertView;
}
class ViewHolder {
}
}