android背景加遮罩,从标题栏下面弹出的popupwindow 带背景遮罩

本文介绍了如何使用PopupWindow在Android中实现标题栏下方弹出带有背景遮罩的效果。通过创建一个全屏PopupWindow并自定义布局,配合背景透明度控制,实现了弹出窗口和遮罩的完美结合。详细步骤包括布局设计、PopupWindow的使用以及显示和消失动画的设置。
摘要由CSDN通过智能技术生成

先上效果  相信许多项目都需要这种效果的

e0e8fe5139b1?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

拿到这个需求  就有了几个解决方案  一种是用 布局隐藏 加 属性动画实现 后来发现这种方法的点击事件比较麻烦  适合从上弹出沾满整个屏幕  像这种有可能的出现一部分的内容的情况就不怎么适应了

所以我果断选择用popupwindow来实现这种需求

说说用popupwindow的思路吧  让popupwindow弹出很容易 可是背景遮罩确实不好加  还需要这样比较美观的  在网上看了看  大概有两种方案  一种是讲popupwindow占满全屏 下面没有内容部分的空间加上遮罩背景   这种方案弹出时明显可以看出整个空间的弹出效果  不太符合要求

第二种方案

privatevoidbackgroundAlpha(floatf) {

WindowManager.LayoutParams lp =getWindow().getAttributes();

lp.alpha = f;

getWindow().setAttributes(lp);

}

这种方法全局加遮罩  可是这样popupwindow的上面的空间也加上了 遮罩  不太美观

下面就说说我实现的思路吧

我将title以下的部分除了显示内容的控件以外 又加了个同样大小的遮罩控件  开始是隐藏的

activity_main.xml  布局

android:layout_width="match_parent"

android:layout_height="match_parent"

android:fitsSystemWindows="true"

android:gravity="top"

android:background="#EFEFF4">

android:id="@+id/line"

android:layout_width="match_parent"

android:layout_height="0.5dp"

android:layout_below="@+id/spread_toolbar"

android:background="#dfdfdf"/>

android:layout_width="match_parent"

android:layout_height="match_parent"

android:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用以下代码来实现弹出PopupWindow,并添背景遮罩以及点击遮罩时弹窗消失。 首先,在你的布局文件(例如activity_main.xml)中添一个透明的背景遮罩视图: ```xml <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- Your main layout content here --> <View android:id="@+id/background_view" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent" /> </FrameLayout> ``` 然后,在你的Activity或Fragment中的代码中,添以下方法来显示和隐藏PopupWindow: ```java public class MainActivity extends AppCompatActivity { private PopupWindow popupWindow; private View backgroundView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); backgroundView = findViewById(R.id.background_view); // 设置背景遮罩点击事件 backgroundView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dismissPopupWindow(); } }); // 显示PopupWindow showPopupWindow(); } private void showPopupWindow() { // 创建PopupWindow视图 LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); View popupView = inflater.inflate(R.layout.popup_window, null); // 设置PopupWindow的属性 popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setFocusable(true); popupWindow.setOutsideTouchable(true); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // 显示PopupWindow popupWindow.showAtLocation(findViewById(R.id.main_layout), Gravity.CENTER, 0, 0); } private void dismissPopupWindow() { // 隐藏PopupWindow if (popupWindow != null && popupWindow.isShowing()) { popupWindow.dismiss(); } } } ``` 最后,创建一个popup_window.xml资源文件来定义PopupWindow的布局: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/white" android:orientation="vertical" android:padding="16dp"> <!-- Your PopupWindow content here --> </LinearLayout> ``` 这样,当你的Activity或Fragment启动时,PopupWindow将显示在屏幕中央,并且点击背景遮罩PopupWindow将被隐藏。请根据你的需要修改布局和样式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值