Android PopupWindow增加半透明蒙層
本文實(shí)例為大家分享了Android PopupWindow增加半透明蒙層的具體代碼,供大家參考,具體內(nèi)容如下
先看效果圖:
實(shí)現(xiàn)代碼:
BasePopupWindowWithMask.class
package com.example.popupwindowwithmask; import android.content.Context; import android.graphics.PixelFormat; import android.graphics.drawable.ColorDrawable; import android.os.IBinder; import android.view.KeyEvent; import android.view.View; import android.view.WindowManager; import android.widget.PopupWindow; /** * Created by kk on 2017/7/22. */ public abstract class BasePopupWindowWithMask extends PopupWindow { protected Context context; private WindowManager windowManager; private View maskView; public BasePopupWindowWithMask(Context context) { super(context); this.context = context; windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); setContentView(initContentView()); setHeight(initHeight()); setWidth(initWidth()); setOutsideTouchable(true); setFocusable(true); setTouchable(true); setBackgroundDrawable(new ColorDrawable()); } protected abstract View initContentView(); protected abstract int initHeight(); protected abstract int initWidth(); @Override public void showAsDropDown(View anchor) { addMask(anchor.getWindowToken()); super.showAsDropDown(anchor); } private void addMask(IBinder token) { WindowManager.LayoutParams wl = new WindowManager.LayoutParams(); wl.width = WindowManager.LayoutParams.MATCH_PARENT; wl.height = WindowManager.LayoutParams.MATCH_PARENT; wl.format = PixelFormat.TRANSLUCENT;//不設(shè)置這個(gè)彈出框的透明遮罩顯示為黑色 wl.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;//該Type描述的是形成的窗口的層級關(guān)系 wl.token = token;//獲取當(dāng)前Activity中的View中的token,來依附Activity maskView = new View(context); maskView.setBackgroundColor(0x7f000000); maskView.setFitsSystemWindows(false); maskView.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { removeMask(); return true; } return false; } }); /** * 通過WindowManager的addView方法創(chuàng)建View,產(chǎn)生出來的View根據(jù)WindowManager.LayoutParams屬性不同,效果也就不同了。 * 比如創(chuàng)建系統(tǒng)頂級窗口,實(shí)現(xiàn)懸浮窗口效果! */ windowManager.addView(maskView, wl); } private void removeMask() { if (null != maskView) { windowManager.removeViewImmediate(maskView); maskView = null; } } @Override public void dismiss() { removeMask(); super.dismiss(); } }
TestPopupWindow.class
package com.example.popupwindowwithmask; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.WindowManager; /** * Created by kk on 2017/7/22. */ public class TestPopupWindow extends BasePopupWindowWithMask { private int[] mIds; private View contentView; private OnItemClickListener listener; public interface OnItemClickListener { void OnItemClick(View v); } public void setOnItemClickListener(OnItemClickListener listener) { this.listener = listener; } public TestPopupWindow(Context context, int[] mIds) { super(context); this.mIds = mIds; initListener(); } @Override protected View initContentView() { contentView = LayoutInflater.from(context).inflate(R.layout.pop_layout, null, false); return contentView; } private void initListener() { for (int i = 0; i < mIds.length; i++) { contentView.findViewById(mIds[i]).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (null != listener) { listener.OnItemClick(v); } dismiss(); } }); } } @Override protected int initHeight() { return WindowManager.LayoutParams.WRAP_CONTENT; } @Override protected int initWidth() { return (int) (0.5 * UIUtils.getScreenWidth(context)); } }
MainActivity.class
package com.example.popupwindowwithmask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.tv_popup); final TestPopupWindow testPopupWindow = new TestPopupWindow(this, new int[]{R.id.pop_location, R.id.pop_group, R.id.pop_list}); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { testPopupWindow.showAsDropDown(textView); } }); testPopupWindow.setOnItemClickListener(new TestPopupWindow.OnItemClickListener() { @Override public void OnItemClick(View v) { switch (v.getId()) { case R.id.pop_location: Toast.makeText(MainActivity.this, "地址", Toast.LENGTH_SHORT).show(); break; case R.id.pop_group: Toast.makeText(MainActivity.this, "分組", Toast.LENGTH_SHORT).show(); break; case R.id.pop_list: Toast.makeText(MainActivity.this, "清單", Toast.LENGTH_SHORT).show(); break; } } }); } }
pop_layout.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <RelativeLayout android:id="@+id/rl_indicator" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="12dp" android:scaleType="fitCenter" android:src="@drawable/filter_arrow_up" /> </RelativeLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="150dp" android:layout_below="@+id/rl_indicator" android:background="@drawable/pop_background" android:gravity="center_horizontal" android:orientation="vertical" android:paddingLeft="15dp" android:paddingRight="15dp"> <TextView android:id="@+id/pop_location" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:drawableLeft="@mipmap/fault_equipment_location_icon" android:drawablePadding="12dp" android:gravity="center_vertical" android:text="地址" android:textColor="#000" android:textSize="16sp" /> <View android:layout_width="match_parent" android:layout_height="0.3dp" android:background="#D2D2D2" /> <TextView android:id="@+id/pop_group" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:drawableLeft="@mipmap/fault_equipment_grouping_icon" android:drawablePadding="12dp" android:gravity="center_vertical" android:text="分組" android:textColor="#000" android:textSize="16sp" /> <View android:layout_width="match_parent" android:layout_height="0.3dp" android:background="#D2D2D2" /> <TextView android:id="@+id/pop_list" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:drawableLeft="@mipmap/fault_equipment_list_icon" android:drawablePadding="12dp" android:gravity="center_vertical" android:text="清單" android:textColor="#000" android:textSize="16sp" /> </LinearLayout> </RelativeLayout> </RelativeLayout>
pop_background.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#ffffff" /> <corners android:radius="5dp" /> </shape>
UIUtils.class
package com.example.popupwindowwithmask; import android.content.Context; /** * Created by kk on 2017/7/22. */ public class UIUtils { /** * 獲得屏幕寬度 * * @param context * @return */ public static int getScreenWidth(Context context) { return context.getResources().getDisplayMetrics().widthPixels; } /** * 獲得屏幕高度 * * @param context * @return */ public static int getScreenHeight(Context context) { return context.getResources().getDisplayMetrics().heightPixels; } }
源碼:下載地址
參考資料:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android輸入框控件ClearEditText實(shí)現(xiàn)清除功能
這篇文章主要為大家詳細(xì)介紹了Android輸入框控件ClearEditText實(shí)現(xiàn)清除功能,感興趣的小伙伴們可以參考一下2016-05-05Grow heap (frag case) 堆內(nèi)存過大的深入解析
本篇文章是對Grow heap (frag case) 堆內(nèi)存過大的問題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06Android?控件自動(dòng)貼邊實(shí)現(xiàn)實(shí)例詳解
這篇文章主要為大家介紹了Android?控件自動(dòng)貼邊實(shí)現(xiàn)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Android10.0實(shí)現(xiàn)本地音樂播放(附源碼下載)
這篇文章主要介紹了Android10.0實(shí)現(xiàn)本地音樂播放(附源碼下載),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Android?kotlin?跳轉(zhuǎn)手機(jī)熱點(diǎn)開關(guān)頁面和判斷熱點(diǎn)是否打開(親測可用)
跳轉(zhuǎn)手機(jī)熱點(diǎn)的頁面肯定是用intent,重點(diǎn)是action不知道是什么,網(wǎng)上最常見的就是Settings.ACTION_WIFI_SETTINGS 跳轉(zhuǎn)wifi設(shè)置頁面,本文介紹Android?kotlin?跳轉(zhuǎn)手機(jī)熱點(diǎn)開關(guān)頁面和判斷熱點(diǎn)是否打開,感興趣的朋友一起看看吧2023-08-08Android獲取RecyclerView滑動(dòng)距離方法詳細(xì)講解
RecyclerView是Android一個(gè)更強(qiáng)大的控件,其不僅可以實(shí)現(xiàn)和ListView同樣的效果,還有優(yōu)化了ListView中的各種不足。其可以實(shí)現(xiàn)數(shù)據(jù)縱向滾動(dòng),也可以實(shí)現(xiàn)橫向滾動(dòng)(ListView做不到橫向滾動(dòng))。接下來講解RecyclerView的用法2023-01-01android換膚功能 如何動(dòng)態(tài)獲取控件中背景圖片的資源id?
這篇文章主要為大家詳細(xì)介紹了android換膚功能中如何動(dòng)態(tài)獲取控件中背景圖片的資源id? ,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08Android仿拉手網(wǎng)團(tuán)購App我的收藏界面實(shí)例代碼
這篇文章主要介紹了Android仿拉手團(tuán)購網(wǎng)App我的收藏界面實(shí)例代碼,需要的朋友可以參考下2017-05-05使用Android開發(fā)接入第三方原生SDK實(shí)現(xiàn)微信登錄
這篇文章主要介紹了使用Android開發(fā)接入第三方原生SDK實(shí)現(xiàn)微信登錄,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03