android window 半透明,popupWindow半透明背景

最近在写关于弹出框需求,特此记录下popupwindow背景透明

网上有关于使用getWindow().getAttributes()设置背景透明,我个人倾向使用setBackgroundDrawable来设置背景头透明。直接上代码

dialog布局

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: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:layout_width="match_parent"

android:layout_height="wrap_content">

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" />

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" />

android:id="@+id/tv_cancel"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:gravity="center_horizontal"

android:padding="12dp"

tools="左边"

app:layout_constraintHorizontal_weight="1"

app:layout_constraintLeft_toLeftOf="parent"

app:layout_constraintRight_toLeftOf="@id/view_"

app:layout_constraintTop_toBottomOf="@+id/view_horizontal" />

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" />

android:id="@+id/tv_confirm"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:gravity="center_horizontal"

android:padding="12dp"

tools:text="右边"

app:layout_constraintHorizontal_weight="1"

app:layout_constraintLeft_toRightOf="@id/view_"

app:layout_constraintRight_toRightOf="parent"

app:layout_constraintTop_toBottomOf="@id/view_horizontal" />

一定要在最外层放置一个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.setContentView(inflate);

//相对于父控件的位置

popupWindow.showAtLocation(mCl, Gravity.CENTER, 0, 0);

重点就是弹出布局要有一个最外层的viewgroup控件包裹内容,其次popup的宽和高设置成match_parent,然后调用setBackgroundDrawable方法。

(第一次写博客,请各位见谅)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. Custom Dialog Android支持自定义窗口的风格: 1)首先在资源里面建立style的value; example: <style name="Theme.CustomDialog" parent="android:style/Theme.Dialog"> <item name="android:windowBackground">@drawable/filled_box</item> </style> drawable/filled_box.xml <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#f0600000"/> <stroke android:width="3dp" color="#ffff8080"/> <corners android:radius="3dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> PS:关于Styles的学习,可以参见:http://code.google.com/android/reference/available-resources.html#stylesandthemes 2)设置当前activity的属性,两种方式:1.在manifest文件中给指定的activity增加属性 android:theme="@android:style/Theme.CustomDialog"。2.在程序中增加语句setTheme(R.style.Theme_CustomDialog); PS1:如果只是将Acticity显示为默认的Dialog, 跳过第一步,只需要在manifest文中增加属性:android:theme="@android:style/Theme.Dialog"或者在程序中增加setTheme(android.R.style.Theme_Dialog). PS2:其他创建Dialog的方法:创建app.Dialog类或者创建app.AlertDialog类。 Next Study:能不能在Activity已经打开以后动态修改当前Activity的风格? 在测试中发现,在onCreate()事件中增加setTheme(),必须在setContentView()之前,否则指定的Style不能生效 2.Custom Title Android除了可以为指定的Activity设置显示风格,此外也可以为指定的Activity设置一些特效,比如自定义Title,没有Title的Activity或者增加一个ICON等。 有意思的一点是,这些特效并不是你想设置的时候就行设置,你需要在Activity显示之前向系统申请要显示的特效,这样才能在下面的程序中为这些特效进行设置。(这样是不是多此一举有待研究) 为一个Activity设置自定义Title的流程: 1)为自定义的Title建立一个layout(custom_title_1.xml) <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/screen" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:id="@+id/left_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:text="Left" /> <TextView android:id="@+id/right_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="Right" /> </RelativeLayout> 关于为什么采用RelativeLayout,可以参见:http://code.google.com/android/devel/ui/layout.html 2)为activity设定自定义Title特效并指定Title的layout: 在onCreate()事件中增加: requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.custom_title); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_1); 这三条语句的次序不能颠倒,依次为申请特效,创建view,设置特效属性。其中requestWindowFeature等价于getWindow().requestFeature() 3)在需要修改Title的地方,获取left_text或者right_text进行设置即可。 Next Study:Activity的其他显示特效 Window还有其他一些feature,比如FEATURE_CONTEXT_MENU,FEATURE_NO_TITLE,FEATURE_LEFT_ICON等,有待继续学习研究。 Translucent Android为透明效果提供了内置的Theme: android:style/Theme.Translucent,只需要把当前的activity的theme设置为这个Theme就可以达到完全透明的效果。 如果要半透明的话,可以增加一个继承该Theme的style即可,实现如下: <style name="Theme.Translucent" parent="android:style/Theme.Translucent"> <item name="android:windowBackground">@drawable/translucent_background</item> <item name="android:colorForeground">#fff</item> </style> <drawable name="translucent_background">#e0000000</drawable> 此外API Demo中提供了另一个实例,不用继承内置的Theme,可以自己完全创建一个新的style,实现透明效果,同时可以加一些其他特效,比如模糊化等,但我试了半天也没有搞定,完全复制代码,也没有出现这个效果,这个现在可能不是很重要的东西,等以后有时间再研究补充吧。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值