【发布时间】:2014-08-23 18:49:55
【问题描述】:
我试图更改按钮背景颜色(默认、按下和焦点),但找不到好方法。我读过很多关于设计按钮样式的帖子,但不幸的是,这些帖子是为了创造一种新的风格。除了背景颜色之外,我希望一切都保持不变。
我更喜欢使用 xml,因为我对多个活动使用单一布局。我也在寻找适用于 android 4.0 及 4.0 及更高版本的解决方案。
有人可以帮帮我吗?
【问题讨论】:
我试图更改按钮背景颜色(默认、按下和焦点),但找不到好方法。我读过很多关于设计按钮样式的帖子,但不幸的是,这些帖子是为了创造一种新的风格。除了背景颜色之外,我希望一切都保持不变。
我更喜欢使用 xml,因为我对多个活动使用单一布局。我也在寻找适用于 android 4.0 及 4.0 及更高版本的解决方案。
有人可以帮帮我吗?
【问题讨论】:
如果你想在不丢失阴影的情况下更改默认按钮颜色,那么我建议更改所需按钮的样式。
您可以通过将此行添加到按钮标签来简单地做到这一点
style="@style/Widget.AppCompat.Button.Colored"
示例代码如下所示
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Widget.AppCompat.Button.Colored"
android:text="Rate now"/>
现在在 res->values->colors.xml 文件下的 color.xml 中将 colorAccent 更改为所需的颜色。
<color name="colorAccent">your color here</color>
您可以访问官方开发者页面以获取更多信息Styling buttons
【讨论】:
使用您想要的按钮设置创建一个button_style.xml,如下例所示,并将此文件放在res 文件夹下的drawable 文件夹中。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="@color/MyButtonDarkGray"
android:endColor="@color/MyButtonDarkGray"
android:angle="270" />
<stroke
android:width="0dp"
android:color="@color/Gray" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item android:state_focused="true" >
<shape>
<gradient
android:endColor="@color/Green"
android:startColor="@color/Green"
android:angle="270" />
<stroke
android:width="0dp"
android:color="@color/Gray" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:endColor="@color/green_leaf_top"
android:startColor="@color/green_leaf_bottom"
android:angle="270" />
<stroke
android:width="0dp"
android:color="@color/Gray" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
现在,在res\values 文件夹下的styles.xml 中,添加
<style name="button" parent="@android:style/Widget.Button">
<item name="android:background">@drawable/button_style</item>
</style>
此样式仅适用于您的所有按钮。
注意:颜色在res\values\colors.xml中单独定义,您可以使用自己的颜色。
【讨论】:
这是一个用于根据按钮状态更改颜色的 xml: 如下创建一个可绘制资源
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="@android:color/black" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="@android:color/holo_orange_dark" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="@android:color/holo_green_dark" />
<item
android:state_enabled="true"
android:drawable="@android:color/holo_blue_bright" />
</selector>
在java代码中设置drawable资源如下:
Button btn = (Button) findViewById(R.id.btn1);
btn.setBackground(this.getResources().getDrawable(R.drawable.mydrawable));
在xml中将drawable设置为xml中按钮的背景:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/mydrawable" />
注意:您可以在 java 代码或 xml 中设置。
【讨论】: