主题不适用于Button的文本

时间:2014-01-20 18:49:09

标签: android button themes

我有一个主题类“Util”,我有3个按钮来更改我的Activity中的主题。 当我应用主题时,每个 TextView 都适用于主题, EditText 不起作用,这使我将其更改为TextView,按钮也不工作 我发现的是文本颜色的字段必须像TextView一样是空的。 我将我的按钮更改为TextView并且工作正常!但有没有办法让这些按钮工作? 这是我的Util类:

    public static void changeToTheme(Activity activity, int theme){
    sTheme = theme;
    activity.finish();

    activity.startActivity(new Intent(activity, activity.getClass()));
}

/** Set the theme of the activity, according to the configuration. */
public static void onActivityCreateSetTheme(Activity activity){
    switch (sTheme)
    {
    default:
    case THEME_DEFAULT:
        activity.setTheme(R.style.FirstTheme);
        break;
    case THEME_WHITE:
        activity.setTheme(R.style.SecondTheme);
        break;
    case THEME_BLUE:
        activity.setTheme(R.style.ThirdTheme);
        break;
    }
}

//Shared Preferences
public static int getTheme()
{
    return sTheme;
}

public static boolean canSetThemeFromPrefs( final Activity activity )
{
    boolean result = false;
    SharedPreferences prefMngr = PreferenceManager.getDefaultSharedPreferences( activity );
    if ( prefMngr.contains( "Theme_Preferences" ) )
    {
        result = true;
    }
    return result;
}

public static int getThemeFromPrefs( final Activity activity )
{
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences( activity );
    final String themeFromPrefs = preferences.getString( "Theme_Preferences", "THEME_DEFAULT" );
    if ( themeFromPrefs.equals( "THEME_BLUE" ) )
    {
        sTheme = THEME_BLUE;
    }
    else if ( themeFromPrefs.equals( "THEME_WHITE" ) )
    {
        sTheme = THEME_WHITE;
    }
    else
    {
        sTheme = THEME_DEFAULT;
    }

    return getTheme();
}

public static int getThemeFromPrefs( final String key )
{
    if ( key.equals( "THEME_BLUE" ) )
    {
        sTheme = THEME_BLUE;
    }
    else if ( key.equals( "THEME_WHITE" ) )
    {
        sTheme = THEME_WHITE;
    }
    else
    {
        sTheme = THEME_DEFAULT;
    }

    return getTheme();
}

这是我的按钮:

            <Button
            android:id="@+id/button3"
            android:layout_width="@dimen/layout_width_numbers"
            android:layout_height="@dimen/layout_width_numbers"
            android:background="@drawable/numbers"
            android:text="@string/button3"
            android:textColor="@color/button_text_color"
            android:textSize="@dimen/button_textsize"
            android:textStyle="bold" />

更新

这是我的 Styles.xml

    <!-- Change Layout theme. -->
<!-- Red. -->
<style name="FirstTheme" >
    <item name="android:textColor">@color/first_theme_button</item>
</style>
<!-- Green. Violet -->
<style name="SecondTheme" >
    <item name="android:textColor">@color/second_theme_button</item>
</style>
<!-- Blue. -->
<style name="ThirdTheme" >
    <item name="android:textColor">@color/third_theme_button</item>
</style>

并且共享偏好不起作用,也不会保存我的上一个主题!!

谢谢:)

1 个答案:

答案 0 :(得分:0)

我测试了这个,我想我知道如何使它工作:

<style name="SecondTheme" parent="android:Theme.Light">
    <item name="android:editTextStyle">@style/SecondTheme</item>
    <item name="android:buttonStyle">@style/SecondTheme</item>

    <item name="android:textColor">@color/second_theme_button</item>
</style>

当我这样做时,我得到了编辑文本和second_theme_button颜色的按钮。