【问题标题】:Themes doesn't apply on Button's Text主题不适用于按钮的文本
【发布时间】:2014-01-20 18:49:09
【问题描述】:

我有一个主题类“Util”,我有 3 个按钮来更改我的 Activity 中的主题。 当我应用主题时,每个 TextView 都与主题配合良好,EditText 无法正常工作,这使我将其更改为 TextView 和 Buttons不工作。 我发现的是文本颜色字段必须像 TextView 一样为空。 我将我的 Button 更改为 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>

而且 Shared Preference 也不起作用,也无法保存我的上一个主题!!

谢谢:)

【问题讨论】:

  • 您的自定义主题中是否可能没有为这些元素定义样式?
  • 那么为什么主题可以使用 TextView 而不能使用 Buttons?

标签: android button themes


【解决方案1】:

我对此进行了测试,我想我知道如何让它发挥作用:

<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 颜色的编辑文本和按钮。

【讨论】:

  • 这是一个有趣的解决方案。它确实工作得很好!!你能帮我解决我的第二个问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-23
相关资源
最近更新 更多