AlertDialog 背景透明

简介: AlertDialog 背景透明
AlertDialog alertDialog;
    void showMagicDialog(int id){
        AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.magicDialog);
        builder.setView(R.layout.dialog_layout);
        alertDialog = builder.create();
        alertDialog.show();
        Window w = alertDialog.getWindow();
        Drawable bgDrawable = new ColorDrawable(0x00000000);
//找到主布局控件, 并修改它的背景       
w.getDecorView().findViewById(getIdFromInternalR("customPanel")).setBackground(bgDrawable);
       //窗体背景修改为透明
        w.setBackgroundDrawable(bgDrawable);
    }


关于getIdFromInternalR("customPanel")


方法在下面.


 

public static int getIdFromInternalR(String idName){
        try {
            Class clasz = Class.forName("com.android.internal.R$id");
            Field field = clasz.getDeclaredField(idName);
            field.setAccessible(true);
            int id= (int)field.get(null);
            return id;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return 0;
    }



如网上说的许多方法, 会调用getWindow().getDecorView().....然后修改背景


但不管用,


后面直接通过


ViewGroup vg = (ViewGroup)getDecorView();
for(int i = 0; i < vg.getChildCount(); i++)


找到ViewGroup则继续往下遍历, 每一个View都setBackgroundDrawable....


最终找到生效的是id/customPanel 和Window本身的background.


详细看源码中的PhoneWindow.java


   PS: 仅在 RK 和 全志 , android4.2, 4.4上面测试通过!!!!


------------ GOOD LUCK ------------------


相关文章
|
Android开发
解决圆形进度条ProgressBar的几个问题
Android自带的Progressbar默认就是圆形的,可以通过设置style属性 style="?android:attr/progressBarStyleHorizontal" 复制代码 这样就能变成条状进度条,如下: <ProgressBar android:layout_width="match_parent" android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal"/>
1038 0
Dialog和DialogFragment 设置背景透明
Dialog和DialogFragment 设置背景透明
887 0
|
Android开发 数据格式 XML
自定义Dialog样式
前言 平时项目开发总要自定义dialog满足产品的设计需求,但系统提供Dialog和AlertDialog用起来不是很方便,所以自己封装一个好用的Dialog基类是再好不过了。
1020 0
|
Android开发
Android弹窗二则: PopupWindow和AlertDialog
前言 弹窗是图形界面必备的一个模块, 回忆一下windows那些恶心爆了的错误弹窗吧, 把弹窗制作的更高效友好一点是非常必要的. 这里说两个常用的弹窗类, PopupWindow和AlertDialog.
1123 0
|
Android开发
[译] 5 分钟让 Drawer 在状态栏下可见
本文讲的是[译] 5 分钟让 Drawer 在状态栏下可见,你也许听过谷歌最新的设计理念 Material Design (“质感设计”)规范,可以让你的抽屉式导航栏跨越整个屏幕,包括状态栏,并且让抽屉后的所有控件以灰暗的网格形式可见。
1130 0
|
Android开发
自定义布局实现侧滑菜单2
我们在上一节已经说了侧滑菜单的实现原理,并且实现了单侧菜单,这一节我们就完善项目,实现双向侧滑菜单。原理我们都说了,不明白的看上节,好了,直接上代码 /** * 这个类和SlidingLayout作用一样,只是没有实现触摸监听事件,直接在121行设置实现了 */ public class SlidingLayout1 extends LinearLayout { /** * 滚动显示和隐藏左侧布局时,手指滑动需要达到的速度。
797 0
|
Android开发
自定义布局实现侧滑菜单1
说起侧滑菜单,应该是一个很Low的话题了,现在几乎所有的app都有这个功能,但是既然大家都在用,那就再说说吧。本文参照郭霖大神的文章,我又重新组织了下,大家可以看原文章。
815 0