以编程方式更改椭圆形按钮背景
Change oval shaped button background programmatically
I want to change Button
background color programmatically the button
shape(oval). I have changed in 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"/>
I want to change
Button
background color programmatically the button shape(oval). I have changed in 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"/>