如何设置的Andr​​oid按钮的背景颜色? [英] How to set Android button background color?

查看:152
本文介绍了如何设置的Andr​​oid按钮的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个自定义背景色为一对夫妇的按钮。我试图的android:背景=#CC2,但它替换整个按钮,所以它看起来并不像一个按钮了。

I would like to use a custom background color for a couple of buttons. I tried android:background="#cc2" but it replaces the entire button, so it doesn't look like a button anymore.

显然可以使用一个<选择> (绘制)这一点。这是唯一的方法吗?我在哪里可以得到一个模板原来,标准的Andr​​oid按钮?

Evidently one can use a <selector> (drawable) for this. Is this the only way? Where do I get a template for the original, standard Android button?

注:接受的答案<一个href=\"http://stackoverflow.com/questions/1521640/standard-android-button-with-a-different-color\">this问题是不是我想要的,因为它看起来并不像一个标准的Andr​​oid按键。

Note: the accepted answer to this question isn't what I want, since it doesn't look like a standard Android button.

推荐答案

根据<一个href=\"http://stackoverflow.com/questions/8369504/why-so-complex-to-set-style-from-$c$c-in-android\">this问题我能够确定,对于例如标准按钮模板Android 2.2的位于

Based on this question I was able to determine that the standard button template for e.g. Android 2.2 is located at

C:\Program Files (x86)\Android-sdk\platforms\android-8\data\res\drawable\btn_default.xml

显然,路径可能是你的电脑上的不同。它指的是可绘如@绘制/ btn_default_normal这是指9个补丁文件,例如在华电国际的是

Obviously, the path may be different on your PC. It refers to drawables such as "@drawable/btn_default_normal" which refer to 9-patch files, e.g. the HDPI ones are

data\res\drawable-hdpi\btn_default_normal.9.png
data\res\drawable-hdpi\btn_default_normal_disable.9.png
data\res\drawable-hdpi\btn_default_pressed.9.png
data\res\drawable-hdpi\btn_default_selected.9.png
data\res\drawable-hdpi\btn_default_normal_disable_focused.9.png

如何寻找资源:我从Button.java的构造开始(使用SDK管理器来下载Android SDK中源):

How to find resources: I started from Button.java's constructor (use SDK Manager to download Sources for Android SDK):

this(context, attrs, com.android.internal.R.attr.buttonStyle);

这是指&LT;项目&gt;在 RES /价值/的themes.xml

<item name="buttonStyle">@android:style/Widget.Button</item>

这是指&LT;风格&gt;在 RES /价值/ styles.xml

<style name="Widget.Button">
    <item name="android:background">@android:drawable/btn_default</item>
    <item name="android:focusable">true</item>
    <item name="android:clickable">true</item>
    <item name="android:textAppearance">?android:attr/textAppearanceSmallInverse</item>
    <item name="android:textColor">@android:color/primary_text_light</item>
    <item name="android:gravity">center_vertical|center_horizontal</item>
</style>

这终于是指 RES /绘制/ btn_default.xml

这篇关于如何设置的Andr​​oid按钮的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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