场景:项目中某种原因 Application 应用的主题是 android:Theme.Light ,而单独某个页面用的是 Theme.AppCompat 。 案例:主 A 页面主题 Theme.Light , B 页面主题 Theme.AppCompat 。现在两处都会弹出 AlertDailog 框
作为喜欢挑战的程序员当然选择方案3,我们来理清楚思路。 1.找到 Theme.Light 主题下 AlertDailog 引用的 style 2.在 AlertDailog.builder 创建的时候指定 style ( Theme )
进入 Theme.Light 查找到与 AlertDialog 相关的属性
该资源非 public 无法被引用,
然后我去<sdk_path>\platforms\android-23\data\res\values\public.xml查找这个属性是否被声明,搜索确实没有被声明。
此时遇到了一个问题,我无法拿到 Theme.Light 下私有的 Theme.Dialog.Alert 资源 id 。
最终经过重重波折,想到了从 A 页面获取 theme 调用 resolveAttribute 解析出 Theme.Dialog.Alert 的资源 id ,然后放到静态变量里。当需要 AlertDailog 的时候就直接用这个解析出来的 Theme.Dialog.Alert 资源 id 。 注意前提是提前解析到这个资源 ID ,否则它解析失败弹出的效果还是主题默认的 dialogTheme 效果。
主 A 页面
public static int holoDialogThemeId=0;
....
final TypedValue outValue = new TypedValue();
getTheme().resolveAttribute(android.R.attr.alertDialogTheme, outValue, true);
holoDialogThemeId = outValue.resourceId;
弹出框使用
AlertDialog.Builder builder=new AlertDialog.Builder(context,holoDialogThemeId);
...