AndroidManifest的activity标签中配置的theme属性的生效流程

AndroidManifest的activity标签中配置的theme属性的生效流程大体如下:
1.android.content.pm.PackageParser.parseActivity()接口中读取AndroidManifest的activity标签中配置的theme属性,代码如下:

a.info.theme = sa.getResourceId(R.styleable.AndroidManifestActivity_theme, 0);

说明:
① a.info为android.content.pm.ActivityInfo对象实例。

2.android.content.pm.ActivityInfo.getThemeResource()接口中获取当前Activity的theme,代码如下:

public final int getThemeResource() {
  return theme != 0 ? theme : applicationInfo.theme;
}

说明:
① 如果当前Activity没有在AndroidManifest中配置theme属性的话,则使用Application的默认theme。

3.android.app.ActivityThread.performLaunchActivity()接口中设置theme,然后调用Activity的onCreate接口,代码如下:

int theme = r.activityInfo.getThemeResource();
if (theme != 0) {
  activity.setTheme(theme);
}

activity.mCalled = false;
if (r.isPersistable()) {
  mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
} else {
  mInstrumentation.callActivityOnCreate(activity, r.state);
}

说明:
① 在AndroidManifest配置的theme,最终也是调用setTheme()接口生效的。
② mInstrumentation.callActivityOnCreate()接口中调用了Activity的onCreate()接口。
③ 从上述逻辑可见,如果需要在Activity代码中调用setTheme()接口的话,推荐在super.onCreate()接口调用之前。

Drawable-night文件通常用于Android应用程序中,当设备处于夜间模式(系统主题为dark时)时加载的主题资源。如果你创建了一个名为`drawable-night`目录下的图片、颜色、状态栏样式等资源,并期望它们在夜间模式下应用,但实际并未生效,可能是以下几个原因: 1. **命名规则错误**:确保你在`drawable-night`目录下的文件名前缀正确地添加了`@drawable/`,例如对于一张图片应该是`@drawable/my_image_name`,而不是`my_image_name`. 2. **主题设置**:确认你的Activity或者Application是否设置了正确的主题。在`styles.xml`中,需要分别定义light和night主题,并启用夜间模式支持。 ```xml <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar"> <!-- ... --> </style> <style name="AppTheme.Night" parent="Theme.MaterialComponents.NoActionBar"> <item name="android:navigationBarColor">@color/your_night_color</item> <!-- 其他夜间主题属性 --> </style> ``` 3. **AndroidManifest.xml**:在`<application>`标签中,如果应用默认启动是白天模式,需要添加条件注解指定夜间模式下启动的主题: ```xml <application android:theme="@style/AppTheme" android:usesCleartextTraffic="true"> <activity android:name=".YourActivity" android:theme="@style/AppTheme.Night" /> <!-- ... --> </application> ``` 4. **代码检查**:如果你是在代码里动态更改主题,确保在切换主题之后刷新了视图或调用了`invalidateOptionsMenu()`。 如果以上都检查过了还是没有效果,你可以尝试清理并重建项目缓存,或者检查日志查看是否有关于资源加载失败的错误信息,以便找到具体的问题所在。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值