我想在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的所有不同版本都具有相同的代码.
解决方法
我最终发现一个按钮实际上是一个9补丁图像,位于@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>