當前位置:首頁 » 安卓系統 » android彈出popupwindow

android彈出popupwindow

發布時間: 2022-09-03 06:08:54

① android popupwindow怎麼合理控制彈出位置

/**
* 計算出來的位置,y方向就在anchorView的上面和下面對齊顯示,x方向就是與屏幕右邊對齊顯示
* 如果anchorView的位置有變化,就可以適當自己額外加入偏移來修正
* @param anchorView 呼出window的view
* @param contentView window的內容布局
* @return window顯示的左上角的xOff,yOff坐標
*/
private static int[] calculatePopWindowPos(final View anchorView, final View contentView) {
final int windowPos[] = new int[2];
final int anchorLoc[] = new int[2];
// 獲取錨點View在屏幕上的左上角坐標位置
anchorView.getLocationOnScreen(anchorLoc);
final int anchorHeight = anchorView.getHeight();
// 獲取屏幕的高寬
final int screenHeight = ScreenUtils.getScreenHeight(anchorView.getContext());
final int screenWidth = ScreenUtils.getScreenWidth(anchorView.getContext());
contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
// 計算contentView的高寬
final int windowHeight = contentView.getMeasuredHeight();
final int windowWidth = contentView.getMeasuredWidth();
// 判斷需要向上彈出還是向下彈出顯示
final boolean isNeedShowUp = (screenHeight - anchorLoc[1] - anchorHeight < windowHeight);
if (isNeedShowUp) {
windowPos[0] = screenWidth - windowWidth;
windowPos[1] = anchorLoc[1] - windowHeight;
} else {
windowPos[0] = screenWidth - windowWidth;
windowPos[1] = anchorLoc[1] + anchorHeight;
}
return windowPos;
}

② android中PopupWindow彈出窗體後,為什麼不能點擊其他控制項

設置popupwindow可點擊
mPopupWindow.setFocusable(true); // 設置PopupWindow可獲得焦點
mPopupWindow.setTouchable(true); // 設置PopupWindow可觸摸
補充:
默認打開popupwindow是沒有焦點和不可點擊的。因此需要設置點擊事件。

③ Android PopupWindow怎麼合理控制彈出位置

Android PopupWindow怎麼合理控制彈出位置

php">privatevoidshowPopupWindow(Viewparent){
if(popupWindow==null){
LayoutInflaterlayoutInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view=layoutInflater.inflate(R.layout.group_list,null);
lv_group=(ListView)view.findViewById(R.id.lvGroup);

Collections.reverse(groups);
GroupAdaptergroupAdapter=newGroupAdapter(this,groups);
lv_group.setAdapter(groupAdapter);
popupWindow=newPopupWindow(view,200,220);
}
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);//設置點擊屏幕其它地方彈出框消失
popupWindow.setBackgroundDrawable(newBitmapDrawable());
WindowManagerwindowManager=(WindowManager)getSystemService(Context.WINDOW_SERVICE);
intxPos=-popupWindow.getWidth()/2
+getCustomTitle().getCenter().getWidth()/2;

popupWindow.showAsDropDown(parent,xPos,4);

lv_group.setOnItemClickListener(newOnItemClickListener(){
@Override
publicvoidonItemClick(AdapterView<?>adapterView,Viewview,
intposition,longid){
loadNew(((StringItem)(groups.get(position))).getId());
if(popupWindow!=null)
popupWindow.dismiss();
}
});
}

只需要設置proupwindows的setOutsideTouchable屬性即可。

以下為示例代碼:

window.showAtLocation(parent, Gravity.RIGHT | Gravity.BOTTOM, 10,10);//顯示位置


第一個參數指定PopupWindow的錨點view,即依附在哪個view上。

第二個參數指定起始點

第三個參數設置以起始點的右下角為原點,向左、上各偏移的像素。

自己看下API

④ android如何彈出一個占屏幕一半的菜單

android彈出一個占屏幕一半的菜單,可以使用popupwindow,設置彈出的xy軸的距離占據屏幕一半即可,如下代碼:

java">packagecom.example.hellopopupwindow;

importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.content.Context;
importandroid.util.Log;
importandroid.view.LayoutInflater;
importandroid.view.MotionEvent;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.view.View.OnTouchListener;
importandroid.view.ViewGroup.LayoutParams;
importandroid.widget.Button;
importandroid.widget.PopupWindow;
importandroid.widget.Toast;

{

privateContextmContext=null;

@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mContext=this;

Buttonbutton=(Button)findViewById(R.id.button);
button.setOnClickListener(newView.OnClickListener(){

@Override
publicvoidonClick(Viewview){

showPopupWindow(view);
}
});
}

privatevoidshowPopupWindow(Viewview){

//一個自定義的布局,作為顯示的內容
ViewcontentView=LayoutInflater.from(mContext).inflate(
R.layout.pop_window,null);
//設置按鈕的點擊事件
Buttonbutton=(Button)contentView.findViewById(R.id.button1);
button.setOnClickListener(newOnClickListener(){

@Override
publicvoidonClick(Viewv){
Toast.makeText(mContext,"buttonispressed",
Toast.LENGTH_SHORT).show();
}
});

finalPopupWindowpopupWindow=newPopupWindow(contentView,
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,true);

popupWindow.setTouchable(true);

popupWindow.setTouchInterceptor(newOnTouchListener(){

@Override
publicbooleanonTouch(Viewv,MotionEventevent){

Log.i("mengdd","onTouch:");

returnfalse;
//這里如果返回true的話,touch事件將被攔截
//攔截後PopupWindow的onTouchEvent不被調用,這樣點擊外部區域無法dismiss
}
});

//如果不設置PopupWindow的背景,無論是點擊外部區域還是Back鍵都無法dismiss彈框
//我覺得這里是API的一個bug
popupWindow.setBackgroundDrawable(getResources().getDrawable(
R.drawable.selectmenu_bg_downward));

//設置好參數之後再show
popupWindow.showAsDropDown(view);

}

}

⑤ 如何在android程序執行初始化的時候彈出一個PopupWindow 觸發的事件是在初始化的時候。

new一個Handler變數,在OnCreate裡面通過這個Handler去發送消息,handler在Onmessage裡面收到消息後,在show出來就可以了;類似的情況還有在oncreate裡面不能顯示toast之類的,要用線程去處理

⑥ android popupwindow 彈出窗口按鈕事件

這個view不能寫在這里,除非你調用這個方法傳入的是MainActivity這種繼承Activity的View
view.getContext().startActivity(intent);

⑦ android service中怎麼彈popupwindow

Android PopupWindow怎麼合理控制彈出位置 void showPopupWindow(View parent) { if (popupWindow == null) { LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = layoutInflater.inflate(R.layout.group_list, null); lv_group = (ListView) view.findViewById(R.id.lvGroup); Collections.reverse(groups); GroupAdapter groupAdapter = new GroupAdapter(this, groups); lv_group.setAdapter(groupAdapter); popupWindow = new PopupWindow(view, 200, 220); } popupWindow.setFocusable(true); popupWindow.setOutsideTouchable(true); //設置點擊屏幕其它地方彈出框消失 popupWindow.setBackgroundDrawable(new BitmapDrawable()); WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); int xPos = -popupWindow.getWidth() / 2 + getCustomTitle().getCenter().getWidth() / 2; popupWindow.showAsDropDown(parent, xPos, 4); lv_group.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { loadNew(((StringItem)(groups.get(position))).getId()); if (popupWindow != null) popupWindow.dismiss(); } }); }只需要設置proupwindows的setOutsideTouchable屬性即可。 以下為示例代碼: window.showAtLocation(parent, Gravity.RIGHT Gravity.BOTTOM, 10,10);//顯示位置 第一個參數指定PopupWindow的錨點view,即依附在哪個view上。 第二個參數指定起始點 第三個參數設置以起始點的右下角為原點,向左、上各偏移的像素。 自己看下API

⑧ Android基礎 (11) PopupWindow詳解

(1)PopupWindow的使用
(2)自定義一個PopupWindow
(3)PopupWindow的源碼分析
(4)AlertDialog,popupWindow,Activity區別
(5)Activity-Window-View三者的差別

Android的對話框有兩種:PopupWindow和AlertDialog。它們的不同點在於:

PopupWindow的位置按照有無偏移分,可以分為偏移和無偏移兩種;按照參照物的不同,可以分為相對於某個控制項(Anchor錨)和相對於父控制項。具體如下:
showAsDropDown(View anchor):相對某個控制項的位置(正左下方),無偏移
showAsDropDown(View anchor, int xoff, int yoff):相對某個控制項的位置,有偏移
showAtLocation(View parent, int gravity, int x, int y):相對於父控制項的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以設置偏移或無偏移。

使用詳述: https://blog.csdn.net/xiaanming/article/details/9121383

這里的WRAP_CONTENT可以換成fill_parent 也可以是具體的數值,它是指PopupWindow的大小,也就是contentView的大小,注意popupWindow根據這個大小顯示你的View,如果你的View本身是從xml得到的,那麼xml的第一層view的大小屬性將被忽略。相當於popupWindow的width和height屬性直接和第一層View相對應。

執行了一個attachToAnchor,意思是PopupWindow類似一個錨掛在目標view的下面,這個函數主要講xoff、yoff(x軸、y軸偏移值)、gravity(比如Gravity.BOTTOM之類,指的是PopupWindow放在目標view哪個方向邊緣的位置)這個attachToAnchor有點意思,通過弱引用保存目標view和目標view的rootView(我們都知道:通過弱引用和軟引用可以防止內存泄漏)、這個rootview是否依附在window、還有保存偏差值、gravity

PopupWindow通過為傳入的View添加一層包裹的布局,並重寫該布局的點擊事件,實現點擊PopupWindow之外的區域PopupWindow消失的效果

封裝庫可前往:#### https://github.com/yangchong211/YCDialog

⑨ Android開發,如何讓PopupWindow彈出時外部控制項不可點擊

// 用於PopupWindow的View 2 View contentView=LayoutInflater.from(context).inflate(layoutRes, null, false); 3 // 創建PopupWindow對象,其中: 4 // 第一個參數是用於PopupWindow中的View,第二個參數是PopupWindow的寬度, 5 // 第三個參數是PopupWindow的高度,第四個參數指定PopupWindow能否獲得焦點 6 PopupWindow window=new PopupWindow(contentView, 100, 100, true); 7 // 設置PopupWindow的背景 8 window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 9 // 設置PopupWindow是否能響應外部點擊事件10 window.setOutsideTouchable(true);11 // 設置PopupWindow是否能響應點擊事件12 window.setTouchable(true);13 // 顯示PopupWindow,其中:14 // 第一個參數是PopupWindow的錨點,第二和第三個參數分別是PopupWindow相對錨點的x、y偏移15 window.showAsDropDown(anchor, xoff, yoff);16 // 或者也可以調用此方法顯示PopupWindow,其中:17 // 第一個參數是PopupWindow的父View,第二個參數是PopupWindow相對父View的位置,18 // 第三和第四個參數分別是PopupWindow相對父View的x、y偏移19 // window.showAtLocation(parent, gravity, x, y);

每個方法的作用都寫在註解里了,相信大家都能看懂。不過這里要注意這兩行:

1 window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));2 window.setOutsideTouchable(true);

只有同時設置PopupWindow的背景和可以響應外部點擊事件,它才能「真正」響應外部點擊事件。也就是說,當你點擊PopupWindow的外部或者按下「Back」鍵時,PopupWindow才會消失。

熱點內容
有哪些低配置游戲像王者榮耀 發布:2024-05-03 22:27:11 瀏覽:243
gp資料庫庫 發布:2024-05-03 22:12:43 瀏覽:873
壓縮點點 發布:2024-05-03 22:12:33 瀏覽:380
有哪些編程比賽 發布:2024-05-03 22:03:45 瀏覽:263
怎麼根據配置調整游戲解析度 發布:2024-05-03 22:02:50 瀏覽:77
小鳥醬265g資源密碼多少啊 發布:2024-05-03 21:32:08 瀏覽:653
三國戰紀游戲華為帳號密碼是多少 發布:2024-05-03 21:22:54 瀏覽:950
變頻壓縮機啟動 發布:2024-05-03 21:17:06 瀏覽:436
建立雲存儲 發布:2024-05-03 21:04:03 瀏覽:76
socket編程php 發布:2024-05-03 20:12:50 瀏覽:209