如何创建可以动态接受角半径和背景颜色的自定义按钮 View ,而不是为每个按钮创建形状文件。 我知道我可以扩展到 Button 类并创建属性集来接受值,但只是不知道如何更改按钮的角半径。
最佳答案
您可以使用MaterialButton
<com.google.android.material.button.MaterialButton
android:id="@+id/btnWithdraw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center"
android:padding="15dp"
android:text="@string/withdraw"
android:textAllCaps="false"
android:textColor="@android:color/white"
android:textSize="15sp"
android:textStyle="bold"
app:backgroundTint="@color/colorLightRed"
app:cornerRadius="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edtAgentPin" />
要更改背景颜色,请使用此
btnWithdraw.backgroundTintList = ColorStateList.valueOf(Color.BLUE)
要更改cornerRadius,请使用此
rootView.btnWithdraw.cornerRadius = 20
注意:确保您已在 Build.Gradle
文件中添加以下依赖项
implementation 'com.google.android.material:material:1.0.0'
关于java - 如何创建可以动态接受角半径和颜色并相应更改的自定义按钮 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57195877/