popupWindow半透明背景

最近在写关于弹出框需求,特此记录下popupwindow背景透明
网上有关于使用getWindow().getAttributes()设置背景透明,我个人倾向使用setBackgroundDrawable来设置背景头透明。直接上代码

dialog布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="38dp"
        android:layout_marginEnd="38dp"
        app:cardBackgroundColor="#ffffff"
        app:cardCornerRadius="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/tv_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="18dp"
                android:text="顶部"
                android:textColor="#000000"
                android:textSize="16sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent" />


            <View
                android:id="@+id/view_horizontal"
                android:layout_width="match_parent"
                android:layout_height="0.5dp"
                android:background="#ccc7c7"
                app:layout_constraintTop_toBottomOf="@id/tv_title" />


            <TextView
                android:id="@+id/tv_cancel"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:padding="12dp"
                android:text="左边"
                app:layout_constraintHorizontal_weight="1"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toLeftOf="@id/view_"
                app:layout_constraintTop_toBottomOf="@+id/view_horizontal" />

            <View
                android:id="@+id/view_"
                android:layout_width="0.5dp"
                android:layout_height="0dp"
                android:background="#c4c1c1"
                app:layout_constraintBottom_toBottomOf="@id/tv_cancel"
                app:layout_constraintHorizontal_weight="0.1"
                app:layout_constraintLeft_toRightOf="@id/tv_cancel"
                app:layout_constraintRight_toLeftOf="@id/tv_confirm"
                app:layout_constraintTop_toBottomOf="@id/view_horizontal"
                app:layout_constraintTop_toTopOf="@id/tv_cancel" />

            <TextView
                android:id="@+id/tv_confirm"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:padding="12dp"
                android:text="右边"
                app:layout_constraintHorizontal_weight="1"
                app:layout_constraintLeft_toRightOf="@id/view_"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/view_horizontal" />

        </android.support.constraint.ConstraintLayout>
    </android.support.v7.widget.CardView>

</android.support.constraint.ConstraintLayout>

一定要在最外层放置一个viewgroup的控件

初始化popup和获取布局

    //获取弹出布局
    inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog, null);
    //初始化控件
    mTvTitle = (TextView) inflate.findViewById(R.id.tv_title);
    mTvTitle.setOnClickListener(this);
    mTvCancel = (TextView) inflate.findViewById(R.id.tv_cancel);
    mTvCancel.setOnClickListener(this);
    mTvConfirm = (TextView) inflate.findViewById(R.id.tv_confirm);
    mTvConfirm.setOnClickListener(this);
    //设置显示的text
    mTvTitle.setText(centerTitle);
    mTvCancel.setText(leftStr);
    mTvConfirm.setText(rightStr);

   //初始化popup,并且设置加载的布局和弹出的宽和高
    popupWindow = new PopupWindow(inflate, ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT);

    //设置背景透明效果(主要)
    popupWindow.setBackgroundDrawable(new ColorDrawable(0x7f000000));
   

    //相对于父控件的位置
    popupWindow.showAtLocation(mCl, Gravity.CENTER, 0, 0);

重点就是弹出布局要有一个最外层的viewgroup控件包裹内容,其次popup的宽和高设置成match_parent,然后调用setBackgroundDrawable方法。
(第一次写博客,请各位见谅)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值