Android风格指定按钮颜色

标签 android xml button background

我正在尝试在 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:drawable/btn_default 的 9-patch 图像,为了更改背景,您必须修改此图像。为了更改按钮文本颜色,我必须设置按钮小部件样式并将其导入到我的应用程序主题中,如下所示:

<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>

关于Android风格指定按钮颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14023601/

相关文章:

cocoa - 删除运行时添加的按钮

android - Bottom Sheet Fragment 附带键盘

java - 创建网站的 webview 时出现错误 : "method does not override or implement a method from a supertype".

Android CTS 测试可以在多个设备上并行运行

c# - XmlIgnore 不起作用

html - 无法在按钮内移动文本

java - Activity 的 AlertDialog 样式按钮

xml - 使用 XSLT 计算 XML 中字符串的出现次数

xml - 理解 XSLT 中的 position() 函数

javascript - 提交后更改表单提交按钮文本