更改按钮颜色而不更改android中的形状

更改按钮颜色而不更改android中的形状

本文介绍了更改按钮颜色而不更改android中的形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我说

 button.setBackgroundColor(Color.BLUE);

按钮形状从默认形状变为矩形.我想更改按钮颜色而不影响其原始形状.请帮助我.

the button shape changes to rectangle from default shape. I want to change the button color without affecting its original shape. Please help me.

推荐答案

每当更改按钮的默认背景时,形状都会发生变化,因为默认形状是矩形,默认背景是带有圆角的可绘制形状.如果您使用任何其他背景,则会丢失圆角效果.

Whenever you change the default background of your button the shape is going to change as the default shape is rectangle and the default background is a shape drawable with rounded corners. If you use any other background this rounded corner effect is lost.

如果使用可绘制的形状作为背景,则可以使用任何颜色或形状实现相同的效果.
这是实现此目的的方法:

You can achieve the same effect with any color or shape if you use a shape drawable as the background.
Here is how you can achieve this:

  1. 创建可绘制形状.
  2. 将此可绘制对象用作按钮的背景.


形状可绘制的示例代码:


sample code for shape drawable:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid
        android:color="#f8f8f8"/>
    <corners
        android:radius="4dp"/>
    <padding
        android:left="10dp"
        android:right="10dp"
        android:top="5dp"
        android:bottom="5dp"/>
    <stroke
        android:width="1dp"
        android:color="#aeaeae"/>
</shape>


如果您想使用带有选择器的按钮,则可以将此xml用作背景

If you want to have a button with a selector then use this xml as the background

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true">
        <shape >
            <solid
                android:color="#ff0000" />
            <stroke
                android:width="1dp"
                android:color="#ff0000" />
            <corners
                android:radius="4dp" />
            <padding
                android:left="10dp"
                android:right="10dp"
                android:top="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item >
        <shape >
            <gradient
                android:startColor="#ff2727"
                android:endColor="#890000"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#620000" />
            <corners
                android:radius="4dp" />
            <padding
                android:left="10dp"
                android:right="10dp"
                android:top="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>

这是一个选择器xml,具有作为不同形状可绘制对象的项目.如果按下按钮,则按钮的状态为 state_pressed ,则使用顶部形状的可绘制对象,否则使用底部形状的可绘制对象.
我希望这会有所帮助.

This is a selector xml with items as the different shape drawables. If the button is pressed that is the button's state is state_pressed then the top shape drawable is used else the bottom shape drawable is used.
I hope this will help.

这篇关于更改按钮颜色而不更改android中的形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 05:36