我有一个 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>
同样,不同的样式有不同的colorPrimary
和 colorPrimaryDark
我 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/