Android setTheme() 不改变 ActionBar 和状态栏

标签 android android-fragments navigation-drawer android-theme android-styles

我有一个 ActionBarActivity其中有抽屉导航的 fragment 。当按下抽屉导航中的项目时,它会启动相应的 fragment 。 在每个 fragment 中,onCreate()方法如下所示:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActivity().setTheme(R.style.AppTheme);

    setHasOptionsMenu(true);
}

然而,每个 fragment 所应用的主题都不同。

我注意到 setTheme()方法似乎没有改变 ActionBar 的颜色或在 styles.xml 中声明的状态栏我从中引用:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/brand_red_primary</item>
    <item name="colorPrimaryDark">@color/brand_red_primaryDark</item>
    <item name="android:actionBarStyle">@style/AppTheme.ActionBarStyle</item>
</style>

同样,不同的样式有不同的colorPrimarycolorPrimaryDark我 Activity 中不同 fragment 的属性。

因此,setTheme()方法不会改变 ActionBar 的颜色或状态栏,但它确实改变了一些东西,例如 ListView 中回调功能的颜色。

我是不是遗漏了什么,或者我需要为 setTheme() 做些什么?更改 ActionBar和状态栏颜色?

更新:

我尝试了发布 here 的解决方案但它给我的结果与我之前尝试的结果完全相同(使用 setTheme() )。

最佳答案

你需要在 super.onCreate(..) 之前调用 setTheme(..),它对我有用:

setTheme(R.style.YourThemeName);
super.onCreate(savedInstanceState);

关于Android setTheme() 不改变 ActionBar 和状态栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29522858/

相关文章:

android - AVD 启动过程中的错误消息

java - 如何将数据从 Recyclerview 传递到 Fragment

android - 在抽屉导航中单击返回时操作栏上的双项

android - 由 Google 插页式广告引起的 ANR

android - 发送短信时 Google AppInvite : Email is not sent,

android - 如何在单击 ListView 项时播放声音?

android - 如何滚动到 ListFragment 的顶部

android - ViewPager 中的 WebViews 并不总是更新

android - 拦截 NavigationUI.onNavDestinationSelected() 使 backstack 弹出 "inclusive = true"

android - 抽屉导航 (menudrawer) Android 5 (lollipop) 样式