Android的风格指定按钮颜色 [英] Android style specify button color

查看:104
本文介绍了Android的风格指定按钮颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在默认情况下为应用按钮的文字颜色的 styles.xml

I am trying to apply a button text color by default in 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> 

如何让我的风格转换按钮颜色以及适用于整个应用程序?我mainfest包括:

How do I make the style change button color, and apply throughout the entire application? My mainfest includes:

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

这改变了所有的文字(比如一个TextView),但在一个按钮不会更改文本。如果我使用ColorThemes风格上面,并将其放置在XML像这样的按钮:

This changes all text (such as a TextView), but does not change the text in a button. If I use the ColorThemes style from above and place it in the xml for the button like this:

    <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 有相同的code。

推荐答案

我最终发现了一个按钮,其实,这是位于@android 9修补形象:为了绘制/ btn_default改变背景你必须修改这个形象。为了改变按钮的文字颜色,我不得不设置一个按钮控件风格,将其导入到我的应用程序的主题,像这样:

I eventually discovered that a button is, in fact, a 9-patch image that is located at @android:drawable/btn_default in order to change the background you must modify this image. In order to change button text color I had to set a button widget style and import it into my app theme like so:

<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的风格指定按钮颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆