如何在运行时更改按钮形状样式?
How to change a button shape style at runtime?
我正在java、android studio 开发一个应用,用户可以在其中选择应用的样式颜色。
大多数组件只需使用 .setBackground(colorUser);
问题出在我的按钮上。
我的按钮都是圆形的,我为此创建了一个形状。
我的形状在其他文件中...
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<solid android:color="@color/colorJetway" />
<corners
android:bottomLeftRadius="80dp"
android:bottomRightRadius="80dp"
android:topLeftRadius="80dp"
android:topRightRadius="80dp" />
</shape>
<Button
android:id="@+id/btnAdc"
android:layout_width="84dp"
android:layout_height="84dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="205dp"
android:background="@drawable/btns_border"
android:onClick="btnAdicionar_click"
android:text="+"
android:textColor="#FFFFFF"
android:textSize="40dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtQuantidade" />
那样的话,如果在运行时我让 mybutton.setBackground(colorUser)
我的按钮失去了它的样式......它不会有圆角的边缘。
我该怎么做?
这不是您要找的。
然而,使用 MaterialButton
组件,圆形按钮非常简单。
只需使用 app:cornerRadius
定义角半径和 app:backgroundTint
更改背景颜色。
<com.google.android.material.button.MaterialButton
app:backgroundTint="@color/myselector"
app:cornerRadius="xxdp"
.../>
您可以使用以下方式以编程方式更改这些值:
button.setCornerRadius(...);
button.setBackgroundTintList(..);
如果您的颜色有限,您可以使用您想要的颜色创建一些可绘制对象。
例如:
blackButton.xml :
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<solid android:color="@color/black" />
<corners
android:bottomLeftRadius="80dp"
android:bottomRightRadius="80dp"
android:topLeftRadius="80dp"
android:topRightRadius="80dp" />
whiteButton.xml :
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<solid android:color="@color/white" />
<corners
android:bottomLeftRadius="80dp"
android:bottomRightRadius="80dp"
android:topLeftRadius="80dp"
android:topRightRadius="80dp" />
当您想要更改按钮的背景时,只需使用 button.setBackground(getResources().getDrawable(R.drawable.blackButton))
或 button.setBackground(getResources().getDrawable(R.drawable.whiteButton))
我正在java、android studio 开发一个应用,用户可以在其中选择应用的样式颜色。
大多数组件只需使用 .setBackground(colorUser);
问题出在我的按钮上。
我的按钮都是圆形的,我为此创建了一个形状。
我的形状在其他文件中...
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<solid android:color="@color/colorJetway" />
<corners
android:bottomLeftRadius="80dp"
android:bottomRightRadius="80dp"
android:topLeftRadius="80dp"
android:topRightRadius="80dp" />
</shape>
<Button
android:id="@+id/btnAdc"
android:layout_width="84dp"
android:layout_height="84dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="205dp"
android:background="@drawable/btns_border"
android:onClick="btnAdicionar_click"
android:text="+"
android:textColor="#FFFFFF"
android:textSize="40dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtQuantidade" />
那样的话,如果在运行时我让 mybutton.setBackground(colorUser)
我的按钮失去了它的样式......它不会有圆角的边缘。
我该怎么做?
这不是您要找的。
然而,使用 MaterialButton
组件,圆形按钮非常简单。
只需使用 app:cornerRadius
定义角半径和 app:backgroundTint
更改背景颜色。
<com.google.android.material.button.MaterialButton
app:backgroundTint="@color/myselector"
app:cornerRadius="xxdp"
.../>
您可以使用以下方式以编程方式更改这些值:
button.setCornerRadius(...);
button.setBackgroundTintList(..);
如果您的颜色有限,您可以使用您想要的颜色创建一些可绘制对象。
例如:
blackButton.xml :
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<solid android:color="@color/black" />
<corners
android:bottomLeftRadius="80dp"
android:bottomRightRadius="80dp"
android:topLeftRadius="80dp"
android:topRightRadius="80dp" />
whiteButton.xml :
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<solid android:color="@color/white" />
<corners
android:bottomLeftRadius="80dp"
android:bottomRightRadius="80dp"
android:topLeftRadius="80dp"
android:topRightRadius="80dp" />
当您想要更改按钮的背景时,只需使用 button.setBackground(getResources().getDrawable(R.drawable.blackButton))
或 button.setBackground(getResources().getDrawable(R.drawable.whiteButton))