使用 Material Components Library:。
将the dependency 添加到您的build.gradle:
dependencies { implementation ‘com.google.android.material:material:1.3.0’ }
在这种情况下,您可以在布局文件中使用MaterialButton:
<com.google.android.material.button.MaterialButton
....
style="@style/Widget.MaterialComponents.Button"
app:cornerRadius=".."
app:strokeColor="@color/colorPrimary"/>
使用app:cornerRadius属性来改变圆角半径的大小。这将圆角指定尺寸。
您还可以使用shapeAppearanceOverlay 属性自定义角。
<style name="MyButton" parent="Widget.MaterialComponents.Button.OutlinedButton">
<item name="shapeAppearanceOverlay">@style/MyShapeAppearance</item>
</style>
<style name="MyShapeAppearance">
<item name="cornerFamily">rounded</item>
<item name="cornerFamilyTopRight">cut</item>
<item name="cornerFamilyBottomRight">cut</item>
<item name="cornerSizeTopLeft">32dp</item>
<item name="cornerSizeBottomLeft">32dp</item>
</style>
官方文档是here 和所有的android 规格here。
使用 Jetpack Compose 1.0.x 使用 shape 参数:
Button( onClick = { /* Do something! */ },
shape = RoundedCornerShape(8.dp)) {
Text("Button")
}
Button(modifier = Modifier.padding(16.dp),
onClick = { /* Do something! */ },
shape = RoundedCornerShape(
50, // topEndPercent
0, // topEndPercent
0, // bottomEndPercent
50. // bottomStartPercent
)
) {
Text("Button")
}
旧支持库:
使用新的Support Library 28.0.0,设计库现在包含Material Button。
您可以将此按钮添加到我们的布局文件中:
<android.support.design.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="XXXXX"
android:textSize="18sp"
android:backgroundTint="@color/colorPrimary"
app:icon="@drawable/ic_android_white_24dp" />
您可以使用此属性设置拐角半径:
-
app:cornerRadius: 用于定义按钮角的半径
dependencies {
implementation 'com.android.support:design:28.0.0'
}