如何设置Android按钮背景颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11023335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set Android button background color?
提问by Qwertie
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.
我想为几个按钮使用自定义背景颜色。我试过了,android:background="#cc2"
但它取代了整个按钮,所以它看起来不再像一个按钮了。
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?
显然,可以<selector>
为此使用(可绘制的)。这是唯一的方法吗?在哪里可以获得原始标准 Android 按钮的模板?
Note: the accepted answer to this questionisn't what I want, since it doesn't look like a standard Android button.
注意:这个问题的公认答案不是我想要的,因为它看起来不像标准的 Android 按钮。
回答by Qwertie
Based on this questionI was able to determine that the standard button template for e.g. Android 2.2 is located at
基于这个问题,我能够确定例如 Android 2.2 的标准按钮模板位于
C:\Program Files (x86)\Android-sdk\platforms\android-8\data\res\drawable\btn_default.xml
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
显然,您的 PC 上的路径可能不同。它指的是可绘制对象,例如"@drawable/btn_default_normal"
指 9 补丁文件的可绘制对象,例如 HDPI 文件是
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
How to find resources: I started from Button.java's constructor (use SDK Manager to download Sources for Android SDK):
如何查找资源:我从 Button.java 的构造函数开始(使用 SDK Manager 下载 Sources for Android SDK):
this(context, attrs, com.android.internal.R.attr.buttonStyle);
Which refers to an <item>
in res/values/themes.xml
:
其中指的是<item>
in res/values/themes.xml
:
<item name="buttonStyle">@android:style/Widget.Button</item>
Which refers to a <style>
in res/values/styles.xml
:
其中指的是一个<style>
in res/values/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>
Which finally refers to res/drawable/btn_default.xml
.
最后指的是res/drawable/btn_default.xml
.
回答by Yar
Add your style
添加您的风格
<style name="MyButtonStyle" parent="android:Widget.Button">
<item name="android:background">@drawable/btn_default</item>
</style>
and your selector (here btn_default.xml)
和你的选择器(这里是 btn_default.xml)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="@drawable/btn_default_normal" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="@drawable/btn_default_normal" />
<item android:state_pressed="true"
android:drawable="@drawable/btn_default_pressed" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="@drawable/btn_default_pressed" />
<item android:state_enabled="true"
android:drawable="@drawable/btn_default_normal" />
<item android:state_focused="true"
android:drawable="@drawable/btn_default_pressed" />
<item
android:drawable="@drawable/btn_default_normal" />
</selector>
and your images (9 patch) btn_default_normal and btn_default_pressed (the names can be different than these) Then, apply the style to your button in xml:
和您的图像(9 个补丁) btn_default_normal 和 btn_default_pressed(名称可以与这些不同)然后,将样式应用于 xml 中的按钮:
<Button
...
style="@style/MyButtonStyle"
/>
回答by Christine
If you change the button background, the entire background is changed, as you noticed. If you want to have a button in your own style or color, you can create a 9-patch file with the graphic of your choice as the button background. If you want it to show selection or click states, you can use a state list with a selector.
如您所见,如果更改按钮背景,则整个背景都会更改。如果您想拥有自己风格或颜色的按钮,您可以创建一个 9-patch 文件,并将您选择的图形作为按钮背景。如果您希望它显示选择或单击状态,您可以使用带有选择器的状态列表。
Both 9patch and selectors are explained here: http://developer.android.com/guide/topics/resources/drawable-resource.html
此处解释了 9patch 和选择器:http: //developer.android.com/guide/topics/resources/drawable-resource.html
回答by FoamyGuy
Where do I get a template for the original, standard Android button?
在哪里可以获得原始标准 Android 按钮的模板?
The system resources are included in the SDK.
系统资源包含在 SDK 中。
you can find them (on windows) at: sdk-root/platforms/[pick a platform]/data/res/drawable/
您可以在以下位置找到它们(在 Windows 上): sdk-root/platforms/[pick a platform]/data/res/drawable/
depending on which platform level you are looking for you may find that the xml selectors you need are in plain drawable, and the png's themselves are in one of the resolution specific folders e.g. drawable-mdpi or drawable-hdpi
根据您正在寻找的平台级别,您可能会发现您需要的 xml 选择器是纯可绘制的,而 png 本身位于分辨率特定的文件夹之一中,例如 drawable-mdpi 或 drawable-hdpi