所以即时尝试为我的应用创建主题,我遇到了一些麻烦。我在我的values / styles.xml中有我的样式,我试图在按钮单击时以编程方式更改。问题是,当我更改主题时,它将我的工具栏样式应用于实际活动。我理解它可能是因为我用Activity.setTheme()改变我的主题;我想知道如果我想将setTheme应用到工具栏上我会用什么?非常感谢任何帮助,谢谢先进!继承我的代码:
(它非常凌乱,最终会被清理掉(这是我的第一个应用程序)) Styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:background">@color/app_theme</item>
</style>
<!-- Customize your theme here. -->
<style name="CustomToolbarStyle" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:toolbarStyle">@style/CustomToolbarStyleMusic</item>
<item name="android:background">@color/white</item>
<item name="android:textColorSecondary">@color/app_theme</item>
<item name="android:windowActionModeOverlay">true</item>
<item name="android:actionModeBackground">@drawable/toolbar_music_theme</item>
<item name="android:actionModeCloseDrawable">@drawable/simple_back_white</item>
<item name="windowActionBar">false</item>
<item name="android:actionModeStyle">@style/MyTheme</item>
</style>
<style name="toolbar_music">
<item name="android:textColor">@color/app_theme</item>
<item name="android:layout_marginTop">2dp</item>
<item name="android:layout_marginRight">-90dp</item>
<item name="android:layout_marginEnd">-80dp</item>
</style>
<style name="toolbar_notes" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:textColor">@color/app_theme</item>
<item name="android:layout_marginTop">25dp</item>
</style>
<style name="CustomToolbarStyleMusic" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:background">@drawable/toolbar_music_theme</item>
<item name="android:textColorSecondary">@color/white</item>
<item name="android:windowActionModeOverlay">true</item>
<item name="android:actionModeBackground">@drawable/toolbar_music_theme</item>
<item name="android:actionModeCloseDrawable">@drawable/simple_back_white</item>
<item name="windowActionBar">false</item>
<item name="android:actionModeStyle">@style/MyTheme</item>
</style>
<!-- menu item theme -->
<style name="CustomPopUpStyle" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:textColor">@color/white</item>
<item name="android:gravity">right</item>
<item name="overlapAnchor">false</item>
<item name="android:dropDownVerticalOffset">-4dp</item>
<item name="android:background">@android:color/transparent</item>
<item name="android:itemBackground">@drawable/simple_brown_tile</item>
<item name="android:dividerHeight">1dp</item>
<item name="android:colorBackground">@color/black</item>
</style>
<style name="MyTheme" parent="@android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">@style/MyTheme.ActionBarStyle</item>
</style>
<style name="MyTheme.ActionBarStyle" parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
</style>
<style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">@color/white</item>
</style>
Utilities.java
private static int sTheme;
public final static int THEME_MAIN = 0;
public final static int THEME_NOTEBOOK = 1;
public static void changeToTheme(Activity activity, int theme) {
sTheme = theme;
activity.finish();
activity.startActivity(new Intent(activity, activity.getClass()));
}
public static void onActivityCreateSetTheme(Activity activity) {
switch (sTheme) {
default:
case THEME_MAIN:
activity.setTheme(R.style.CustomToolbarStyleMusic);
break;
case THEME_NOTEBOOK:
activity.setTheme(R.style.CustomToolbarStyle);
break;
}
}