當前位置:編程學習大全網 - 源碼下載 - android 怎麽自定popupwindow設置高度無反應

android 怎麽自定popupwindow設置高度無反應

使用PopupWindow可實現彈出窗口效果,,其實和AlertDialog壹樣,也是壹種對話框,兩者也經常混用,但是也各有特點。下面就看看使用方法。

首先初始化壹個PopupWindow,指定窗口大小參數。

PopupWindow mPop = new PopupWindow(getLayoutInflater().inflate(R.layout.window, null),

LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

也可以分開寫:

LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

//自定義布局

ViewGroup menuView = (ViewGroup) mLayoutInflater.inflate(

R.layout.window, null, true);

PopupWindow mPop = new PopupWindow(menuView, LayoutParams.WRAP_CONTENT,

LayoutParams.WRAP_CONTENT, true);

當然也可以手動設置PopupWindow大小。

mPop.setContentView(menuView );//設置包含視圖

mPop.setWidth(int )

mPop.setHeight(int )//設置彈出框大小

設置進場動畫:

mPop.setAnimationStyle(R.style.AnimationPreview);//設置動畫樣式

mPop.setOutsideTouchable(true);//這裏設置顯示PopuWindow之後在外面點擊是否有效。如果為false的話,那麽點擊PopuWindow外面並不會關閉PopuWindow。當然這裏很明顯只能在Touchable下才能使用。

當有mPop.setFocusable(false);的時候,說明PopuWindow不能獲得焦點,即使設置設置了背景不為空也不能點擊外面消失,只能由dismiss()消失,但是外面的View的事件還是可以觸發,back鍵也可以順利dismiss掉。當設置為popuWindow.setFocusable(true);的時候,加上下面兩行設置背景代碼,點擊外面和Back鍵才會消失。

mPop.setFocusable(true);

需要順利讓PopUpWindow dimiss(即點擊PopuWindow之外的地方此或者back鍵PopuWindow會消失);PopUpWindow的背景不能為空。必須在popuWindow.showAsDropDown(v);或者其它的顯示PopuWindow方法之前設置它的背景不為空:

mPop.setBackgroundDrawable(new ColorDrawable(0));

mPop.showAsDropDown(anchor, 0, 0);//設置顯示PopupWindow的位置位於View的左下方,x,y表示坐標偏移量

mPop.showAtLocation(findViewById(R.id.parent), Gravity.LEFT, 0, -90);(以某個View為參考),表示彈出窗口以parent組件為參考,位於左側,偏移-90。

mPop.setOnDismissListenerd(new PopupWindow.OnDismissListener(){})//設置窗口消失事件

註:window.xml為布局文件

總結:

1、為PopupWindow的view布局,通過LayoutInflator獲取布局的view.如:

LayoutInflater inflater =(LayoutInflater)

this.anchor.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View textEntryView = inflater.inflate(R.layout.paopao_alert_dialog, null);

2、顯示位置,可以有很多方式設置顯示方式

pop.showAtLocation(findViewById(R.id.ll2), Gravity.LEFT, 0, -90);

或者

pop.showAsDropDown(View anchor, int xoff, int yoff)

3、進出場動畫

pop.setAnimationStyle(R.style.PopupAnimation);

4、點擊PopupWindow區域外部,PopupWindow消失

this.window = new PopupWindow(anchor.getContext());

this.window.setTouchInterceptor(new OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

if(event.getAction() ==MotionEvent.ACTION_OUTSIDE) {

BetterPopupWindow.this.window.dismiss();

return true;

}

return false;

}

});

PopupWindow 自適應寬度實例

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

int maxWidth = meathureWidthByChilds() + getPaddingLeft() + getPaddingRight(); super.onMeasure(MeasureSpec.makeMeasureSpec(maxWidth,MeasureSpec.EXACTLY),heightMeasureSpec);

}

public int meathureWidthByChilds() {

int maxWidth = 0;

View view = null;

for (int i = 0; i < getAdapter().getCount(); i++) {

view = getAdapter().getView(i, view, this);

view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);

if (view.getMeasuredWidth() > maxWidth){

maxWidth = view.getMeasuredWidth();

}

}

return maxWidth;

}

PopupWindow自適應布局

Android自帶的Menu菜單,常常無法滿足我們的需求,所以就只有自己寫menu菜單,通常的選擇是用PopupWindow來實現自定義的menu菜單,先看代碼,再來說明要註意的幾點:

View menuView = inflater.inflate(R.layout.menu_popwindow, null);

final PopupWindow p = new PopupWindow(mContext);

p.setContentView(menuView);

p.setWidth(ViewGroup.LayoutParams.FILL_PARENT);

p.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

p.setAnimationStyle(R.style.MenuWindow);

p.setOnDismissListener(this);

p.setOutsideTouchable(false);

p.setBackgroundDrawable(new ColorDrawable(android.R.color.transparent));

p.setFocusable(true); // 如果把焦點設置為false,則其他部份是可以點擊的,也就是說傳遞事件時,不會先走PopupWindow

mPopwindow = p;

  • 上一篇:照片直播怎麽弄?
  • 下一篇:請問大哥如何用C#鏈表實現壹元多項式的加減呀,給個源代碼吧
  • copyright 2024編程學習大全網