以编程方式更改椭圆形按钮背景
问题描述:
我想以编程方式更改
Button
背景色按钮 shape(oval)。我改变了XML。以编程方式更改椭圆形按钮背景
这是代码:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="20dp"/>
<solid android:color="#f9f9f9"/>
<stroke
android:width="2dp" android:color="#FFFF4917" />
</shape>
我使用Button.backgroundColor(COLOR.RED)
改变这种Button
但问题是,它改变按钮的颜色,但它使我的按钮矩形(默认设置形状按钮)
答
你可以使用这个简单的方法向下跌破
GradientDrawable BackgroundShape = (GradientDrawable)btn.getBackground();
BackgroundShape.setColor(Color.BLACK);
修改
答
使用它。当点击按钮时,它会让你的椭圆形的形状。
style_login_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape android:shape="oval">
<solid android:color="@color/colorDeepOrange"/>
<size android:width="120dp" android:height="120dp"/>
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="oval">
<solid android:color="@color/colorOrange"/>
<size android:width="120dp" android:height="120dp"/>
</shape>
</item>
<item >
<shape android:shape="oval">
<solid android:color="@color/colorOrange"/>
<size android:width="120dp" android:height="120dp"/>
</shape>
</item>
</selector>
,并适用于Button
。像 -
<Button
android:id="@+id/btnSignin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_btn_login"
android:textColor="@color/colorWhite"
android:textSize="25dp"
android:padding="30dp"
android:textAllCaps="false"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:background="@drawable/style_login_button"/>
+0
他想要使用代码以编程方式将按钮设置为椭圆形。 – Rab
感谢working.will 10分钟后投它 – Meghna
编码愉快:)顺便说一下我编辑你的问题,你已经在你原来做的拼写错误。 – Rab