在Android应用程序中,我想更改Button
的背景颜色。按钮的背景
我试图将android:Drawable/btn_default_holo_light
的“背景”属性更改为#FFFF00
,并根据需要获得了新颜色。缺点是Button
的形状也发生了变化:原来的Button
的边距消失了,彩色区域变得越来越大,完全填满了它的区域。
如何更改背景颜色而不是Button
的外观?
在Android应用程序中,我想更改Button
的背景颜色。按钮的背景
我试图将android:Drawable/btn_default_holo_light
的“背景”属性更改为#FFFF00
,并根据需要获得了新颜色。缺点是Button
的形状也发生了变化:原来的Button
的边距消失了,彩色区域变得越来越大,完全填满了它的区域。
如何更改背景颜色而不是Button
的外观?
您可以设置按钮背景使用自定义编程可绘
b= (Button) findViewById(R.id.button1);
b.setBackgroundColor(Color.RED);
设置背景。
buttonbkg.xml在绘制文件夹
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/pressed" />
<item android:state_focused="false"
android:drawable="@drawable/normal" />
</selector>
在绘制文件夹中的.xml正常
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<stroke android:width="3dp"
android:color="#0FECFF" /><!-- #330000FF #ffffffff -->
<gradient
android:startColor="#ffffffff"
android:endColor="#110000FF"
android:angle="90"/>
<padding android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp"/>
<corners android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
</shape>
pressed.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FF1A47"/>
<stroke android:width="3dp"
android:color="#0FECFF"/>
<padding android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp"/>
<corners android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
在你activity_main.xml中
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:background="@drawable/buttonbkg" or android:background="#0EFFFF"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Click" />
由于Button形状是在其背景图像中定义的,因此不能仅更改背景色。如果你想要一个不同的颜色,那么你需要创建自己的背景。只是谷歌的“android自定义按钮”,你会发现吨的信息和教程。
那么按钮标签中只有android:backgroud=#FFFF00
。我认为这将保持可绘制的主题,因此保持利润率,但改变颜色。或者这是你已经尝试过的吗?
谢谢。我不认为这个问题已经回答了所问的问题,但这些信息仍然有用! –