Android PopupWindow使用,下拉式PopupWindow,底部式PopupWindow
生活随笔
收集整理的這篇文章主要介紹了
Android PopupWindow使用,下拉式PopupWindow,底部式PopupWindow
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、實現方法1? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 仿微信盆友圈彈出點贊、評論
? ?
demo連接:android開發PopupWindow實現跟隨試彈出框-Android文檔類資源-CSDN下載
實現步驟
1、下載module并引入項目
引入module步驟:Android studio 導入module方法_meixi_android的博客-CSDN博客_as怎么導入module
2、實現方法2
1、封裝彈出窗類
/*** 作者:created by meixi* 郵箱:13164716840@163.com* 日期:2018/10/26 15*/public abstract class CommonPopupWindow {protected Context context;protected View contentView;protected PopupWindow mInstance;public CommonPopupWindow(Context c, int layoutRes, int w, int h) {context=c;contentView= LayoutInflater.from(c).inflate(layoutRes, null, false);initView();initEvent();mInstance=new PopupWindow(contentView, w, h, true);initWindow();}public View getContentView() { return contentView; }public PopupWindow getPopupWindow() { return mInstance; }protected abstract void initView();protected abstract void initEvent();protected void initWindow() {mInstance.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));mInstance.setOutsideTouchable(true); // mInstance.setTouchable(true);}public void showBashOfAnchor(View anchor, LayoutGravity layoutGravity, int xmerge, int ymerge) {int[] offset=layoutGravity.getOffset(anchor, mInstance);mInstance.showAsDropDown(anchor, offset[0]+xmerge, offset[1]+ymerge);}public void showAsDropDown(View anchor, int xoff, int yoff) {mInstance.showAsDropDown(anchor, xoff, yoff);}public void showAtLocation(View parent, int gravity, int x, int y) {mInstance.showAtLocation(parent, gravity, x, y);}public static class LayoutGravity {private int layoutGravity;// waring, don't change the order of these constants!public static final int ALIGN_LEFT=0x1;public static final int ALIGN_ABOVE=0x2;public static final int ALIGN_RIGHT=0x4;public static final int ALIGN_BOTTOM=0x8;public static final int TO_LEFT=0x10;public static final int TO_ABOVE=0x20;public static final int TO_RIGHT=0x40;public static final int TO_BOTTOM=0x80;public static final int CENTER_HORI=0x100;public static final int CENTER_VERT=0x200;public LayoutGravity(int gravity) {layoutGravity=gravity;}public int getLayoutGravity() { return layoutGravity; }public void setLayoutGravity(int gravity) { layoutGravity=gravity; }public void setHoriGravity(int gravity) {layoutGravity&=(0x2+0x8+0x20+0x80+0x200);layoutGravity|=gravity;}public void setVertGravity(int gravity) {layoutGravity&=(0x1+0x4+0x10+0x40+0x100);layoutGravity|=gravity;}public boolean isParamFit(int param) {return (layoutGravity & param) > 0;}public int getHoriParam() {for(int i=0x1; i<=0x100; i=i<<2)if(isParamFit(i))return i;return ALIGN_LEFT;}public int getVertParam() {for(int i=0x2; i<=0x200; i=i<<2)if(isParamFit(i))return i;return TO_BOTTOM;}public int[] getOffset(View anchor, PopupWindow window) {int anchWidth=anchor.getWidth();int anchHeight=anchor.getHeight();int winWidth=window.getWidth();int winHeight=window.getHeight();View view=window.getContentView();if(winWidth<=0)winWidth=view.getWidth();if(winHeight<=0)winHeight=view.getHeight();int xoff=0;int yoff=0;switch (getHoriParam()) {case ALIGN_LEFT:xoff=0; break;case ALIGN_RIGHT:xoff=anchWidth-winWidth; break;case TO_LEFT:xoff=-winWidth; break;case TO_RIGHT:xoff=anchWidth; break;case CENTER_HORI:xoff=(anchWidth-winWidth)/2; break;default:break;}switch (getVertParam()) {case ALIGN_ABOVE:yoff=-anchHeight; break;case ALIGN_BOTTOM:yoff=-winHeight; break;case TO_ABOVE:yoff=-anchHeight-winHeight; break;case TO_BOTTOM:yoff=0; break;case CENTER_VERT:yoff=(-winHeight-anchHeight)/2; break;default:break;}return new int[]{ xoff, yoff };}} }外部不可點擊
mInstance.setOutsideTouchable(false); mInstance.setFocusable(false);2、activity調用代碼
(1)下拉式彈出
private CommonPopupWindow.LayoutGravity layoutGravity;private CommonPopupWindow window; private View activityPopup; activityPopup=findViewById(R.id.activity_popup); textView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {PopupWindow win=window.getPopupWindow();win.setAnimationStyle(R.style.animTranslate);//動畫效果window.showBashOfAnchor(textView, layoutGravity, 0, 0);//跟隨式彈出 // window.showAtLocation(activityPopup, Gravity.BOTTOM, 0, 0);//底部彈出WindowManager.LayoutParams lp=getWindow().getAttributes();lp.alpha=0.3f;getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);getWindow().setAttributes(lp);}});下拉式實例化
private void initPopup2(){layoutGravity=new CommonPopupWindow.LayoutGravity(CommonPopupWindow.LayoutGravity.CENTER_HORI| CommonPopupWindow.LayoutGravity.TO_BOTTOM);DisplayMetrics dm = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(dm);int nMultiple = dm.widthPixels;//寬window=new CommonPopupWindow(this, R.layout.mypopup, (int) (nMultiple*0.4), ViewGroup.LayoutParams.WRAP_CONTENT) {@Overrideprotected void initView() {View view=getContentView();linearLayout =(LinearLayout)view.findViewById(R.id.oneli);}@Overrideprotected void initEvent() {linearLayout.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Toast.makeText(MainActivity.this,"ssssblue",Toast.LENGTH_SHORT).show();}});}@Overrideprotected void initWindow() {super.initWindow();PopupWindow instance=getPopupWindow();instance.setOnDismissListener(new PopupWindow.OnDismissListener() {@Overridepublic void onDismiss() {WindowManager.LayoutParams lp=getWindow().getAttributes();lp.alpha=1.0f;getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);getWindow().setAttributes(lp);}});}}; }(2)底部彈出
private void initPopupWindow() {DisplayMetrics dm = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(dm);int nMultiple = dm.widthPixels;//寬// window=new CommonPopupWindow(this, R.layout.mypopup, ViewGroup.LayoutParams.MATCH_PARENT, (int) (screenHeight*0.7)) {window=new CommonPopupWindow(this, R.layout.mypopup, (int) (nMultiple*0.9), ViewGroup.LayoutParams.WRAP_CONTENT) {@Overrideprotected void initView() {View view=getContentView();linearLayout =(LinearLayout)view.findViewById(R.id.oneli);}@Overrideprotected void initEvent() {linearLayout.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Toast.makeText(MainActivity.this,"ssssblue",Toast.LENGTH_SHORT).show();}});}@Overrideprotected void initWindow() {super.initWindow();PopupWindow instance=getPopupWindow();instance.setOnDismissListener(new PopupWindow.OnDismissListener() {@Overridepublic void onDismiss() {WindowManager.LayoutParams lp=getWindow().getAttributes();lp.alpha=1.0f;getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);getWindow().setAttributes(lp);}});}};} window.getPopupWindow().dismiss();//隱藏demo鏈接:PopupWindow實現demo-Android代碼類資源-CSDN下載
下拉式彈出窗??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 全屏底部彈出窗
? ? ? ? ?
總結
以上是生活随笔為你收集整理的Android PopupWindow使用,下拉式PopupWindow,底部式PopupWindow的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python ssh
- 下一篇: Android精准计步器