【问题标题】:Android style specify button colorAndroid 样式指定按钮颜色
【发布时间】:2012-12-11 00:12:53
【问题描述】:

我正在尝试在 styles.xml

中默认应用按钮文本颜色
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme">
   <item name="android:windowFullscreen">true</item>
   <item name="android:windowBackground">@color/black</item>
   <item name="android:textColor">@color/green</item>
</style> 

如何使样式更改按钮颜色,并应用于整个应用程序?我的主要活动包括:

    <application
    android:icon="@drawable/ic_launcher"
    android:label="Hack the World"
    android:theme="@style/AppTheme">

这会更改所有文本(例如 TextView),但不会更改按钮中的文本。如果我使用上面的 ColorThemes 样式并将其放在按钮的 xml 中,如下所示:

    <Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:onClick="loadSavedgame"
    android:text="Load Saved Game" 
    android:textColor="@color/green" />"

然后它完美地工作。为什么由于风格而不能普遍适用?所有不同版本的 styles.xml 都有相同的代码。

【问题讨论】:

    标签: android xml button background


    【解决方案1】:

    我最终发现一个按钮实际上是一个 9-patch 图像,位于 @android:drawable/btn_default 为了改变背景你必须修改这个图像。为了更改按钮文本颜色,我必须设置按钮小部件样式并将其导入我的应用主题,如下所示:

    <resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar" >
       <item name="android:windowFullscreen">true</item>
       <item name="android:windowBackground">@color/black</item>
       <item name="android:textColor">@color/green</item>
       <item name="android:buttonStyle">@style/ButtonText</item>
    </style> 
    
        <style name="ButtonText" parent="@android:style/Widget.Button">
       <item name="android:textColor">@color/green</item>
       <item name="android:background">@android:drawable/btn_default</item><!-- only way to change this is to change the src image -->
       <item name="android:layout_width">80.0px</item>
       <item name="android:layout_height">40.0px</item> 
    </style> 
    </resources>
    

    【讨论】:

      猜你喜欢
      • 2015-05-11
      • 1970-01-01
      • 1970-01-01
      • 2012-09-14
      • 1970-01-01
      • 2016-09-11
      • 1970-01-01
      • 1970-01-01
      • 2020-06-17
      相关资源
      最近更新 更多