微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android风格指定按钮颜色

我想在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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐