gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 16:15:26 24 4
gpt4 key购买 nike

我有一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com