您也可以使用Material Components for Android。
将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.OutlinedButton"
app:cornerRadius=".."
app:strokeColor="@color/colorPrimary"/>
应用样式@style/Widget.MaterialComponents.Button.OutlinedButton。
在您的情况下,使用 app:cornerRadius 属性来更改拐角半径的大小。这将圆角指定尺寸。
使用 te 属性 app:strokeColor 和 app:strokeWidth 更改边框的颜色和宽度。
您还可以使用ShapeApperance 自定义角(需要1.1.0 版本)
<style name="MyButton" parent="Widget.MaterialComponents.Button.OutlinedButton">
<item name="shapeAppearanceOverlay">@style/MyShapeAppearance</item>
</style>
<style name="MyShapeAppearance" parent="">
<item name="cornerFamilyTopLeft">rounded</item>
<item name="cornerFamilyBottomLeft">rounded</item>
<item name="cornerFamilyTopRight">cut</item>
<item name="cornerFamilyBottomRight">cut</item>
<item name="cornerSize">8dp</item>
</style>
官方文档是here 和所有的android 规格here。
使用 jetpack compose 1.0.x 您可以使用 OutlinedButton 和 border 属性:
OutlinedButton(
onClick = { },
border = BorderStroke(1.dp, Color.Blue),
shape = RoundedCornerShape(8.dp)
) {
Text(text = "Save")
}
OLD(支持库)
使用新的Support Library 28.0.0,设计库现在包含Material Button。
您可以将此按钮添加到我们的布局文件中:
<android.support.design.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="XXXX"
android:textSize="18sp"
app:icon="@drawable/ic_android_white_24dp" />
您可以使用以下属性自定义按钮:
还有