如何以编程方式修复 android 上的圆角按钮?
How to fix programmatically round corners buttons on android?
我想像下面的代码那样在按钮上做圆角
button.setWidth(buttonWidth);
button.setHeight(buttonHeight);
如何用java代码定义圆角按钮,没有xml
您可以使用您想要的圆角矩形背景制作一个 xml 可绘制对象(如果您希望它在按下时突出显示,请确保使用可绘制状态列表)并将其设置为背景。设置为背景只是一个setBackgroundDrawable的调用。
首先创建背景:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<gradient android:angle="-90" android:startColor="#345953" android:endColor="#689a92" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<solid android:color="#58857e"/>
</shape>
</item>
<item >
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<gradient android:angle="-90" android:startColor="#8dbab3" android:endColor="#58857e" />
</shape>
</item>
然后:
button.setBackground(ContextCompat.getDrawable(context, R.drawable.ready));
此答案基于 this and this link.
使用 Material Components for Android.
只需添加依赖:
implementation ‘com.google.android.material:material:1.0.0’
在您的布局中添加 MaterialButton
:
<com.google.android.material.button.MaterialButton
....
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
app:cornerRadius=".."
app:strokeColor="@color/colorPrimary"/>
并使用方法 setCornerRadius
.
类似于:
button.setCornerRadius(..);
只需将您的主题设置为 MaterialComponents。请注意,这会弄乱您当前的主题。
https://learntodroid.com/how-to-create-rounded-corners-for-a-button-in-android/ 可能是另一种选择。
我想像下面的代码那样在按钮上做圆角
button.setWidth(buttonWidth);
button.setHeight(buttonHeight);
如何用java代码定义圆角按钮,没有xml
您可以使用您想要的圆角矩形背景制作一个 xml 可绘制对象(如果您希望它在按下时突出显示,请确保使用可绘制状态列表)并将其设置为背景。设置为背景只是一个setBackgroundDrawable的调用。
首先创建背景:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<gradient android:angle="-90" android:startColor="#345953" android:endColor="#689a92" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<solid android:color="#58857e"/>
</shape>
</item>
<item >
<shape android:shape="rectangle" >
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<gradient android:angle="-90" android:startColor="#8dbab3" android:endColor="#58857e" />
</shape>
</item>
然后:
button.setBackground(ContextCompat.getDrawable(context, R.drawable.ready));
此答案基于 this and this link.
使用 Material Components for Android.
只需添加依赖:
implementation ‘com.google.android.material:material:1.0.0’
在您的布局中添加 MaterialButton
:
<com.google.android.material.button.MaterialButton
....
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
app:cornerRadius=".."
app:strokeColor="@color/colorPrimary"/>
并使用方法 setCornerRadius
.
类似于:
button.setCornerRadius(..);
只需将您的主题设置为 MaterialComponents。请注意,这会弄乱您当前的主题。
https://learntodroid.com/how-to-create-rounded-corners-for-a-button-in-android/ 可能是另一种选择。