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