您还可以使用Android 的材质组件 https://github.com/material-components/material-components-android.
Add 依赖性 https://github.com/material-components/material-components-android/blob/master/docs/getting-started.md给你的build.gradle
:
dependencies { implementation 'com.google.android.material:material:1.3.0' }
在这种情况下,您可以使用MaterialButton https://github.com/material-components/material-components-android/blob/master/docs/components/MaterialButton.md在你的布局文件中:
<com.google.android.material.button.MaterialButton
....
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
app:cornerRadius=".."
app:strokeColor="@color/colorPrimary"/>
应用样式@style/Widget.MaterialComponents.Button.OutlinedButton http://@style/Widget.MaterialComponents.Button.OutlinedButton.
In your case use the app:cornerRadius
attribute to change the size of corner radius. This will round off the corners with specified dimensions.
Use te attribute app:strokeColor
and app:strokeWidth
to change the color and the width of the border.
您还可以使用自定义角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 https://developer.android.com/reference/com/google/android/material/button/MaterialButton以及所有 Android 规格here https://material.io/develop/android/components/material-button/.
With 喷气背包组成你可以使用OutlinedButton
和border
属性:
OutlinedButton(
onClick = { },
border = BorderStroke(1.dp, Color.Blue),
shape = RoundedCornerShape(8.dp)
) {
Text(text = "Save")
}
旧(支持库)
随着新支持库28.0.0 https://developer.android.com/topic/libraries/support-library/revisions.html#28-0-0-alpha1,设计库现在包含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" />
您可以使用以下属性自定义按钮:
-
app:backgroundTint
: 用于对按钮的背景应用色调。如果您想更改按钮的背景颜色,请使用此属性而不是背景。
-
app:strokeColor
:按钮描边使用的颜色
-
app:strokeWidth
: 按钮行程所使用的宽度
Also