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

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值