我写了一个代码来设置活动的主题 setTheme(R.style.darkTheme); ,但是我不知道如何编写片段活动来设置主题。活动
if(sharedPref.loadNightModeState()==true)
{
setTheme(R.style.darkTheme);
}
else {
setTheme(R.style.AppTheme);
}
这是我的style.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="backgroundcolor">@color/background</item>
<item name="textcolor">@color/textcolor</item>
<item name="buttoncolor">@color/red</item>
</style>
<style name="darkTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#212121</item>
<item name="colorPrimaryDark">#000000</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="backgroundcolor">#303030</item>
<item name="textcolor">@color/white</item>
<item name="buttoncolor">@color/red</item>
</style>
</resources>
这是我的片段代码
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_state, container, false);
sharedPref=new SharedPref(getActivity());
if(sharedPref.loadNightModeState()==true)
{
setTheme(R.style.darkTheme);
}
else {
setTheme(R.style.AppTheme);
}
return view;
}
此setTheme(R.style.AppTheme)行中出现错误。