你可以使用
在您的build.gradle
基础上再加the dependency:
dependencies { implementation ‘com.google.android.material:material:1.3.0’ }
然后将MaterialButton
添加到布局中:
<com.google.android.material.button.MaterialButton
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
app:strokeColor="@color/colorAccent"
app:strokeWidth="6dp"
app:layout_constraintStart_toStartOf="parent"
app:shapeAppearance="@style/MyShapeAppearance"
/>
你可以在这里查full documentation和API here.
要更改background color,您有两个 Select .
- 使用100属性.
类似于:
<style name="MyButtonStyle"
parent="Widget.MaterialComponents.Button">
<item name="backgroundTint">@color/button_selector</item>
//..
</style>
- 在我看来,这将是最好的 Select .如果您想覆盖默认样式中的一些主题属性,那么您可以使用新的100属性.
类似于:
<style name="MyButtonStyle"
parent="Widget.MaterialComponents.Button">
<item name=“materialThemeOverlay”>@style/GreenButtonThemeOverlay</item>
</style>
<style name="GreenButtonThemeOverlay">
<!-- For filled buttons, your theme's colorPrimary provides the default background color of the component -->
<item name="colorPrimary">@color/green</item>
</style>
选项#2至少需要1.1.0
版.
您可以使用以下样式之一:
- Filled Button (default):
style="@style/Widget.MaterialComponents.Button
- Text Button:
style="@style/Widget.MaterialComponents.Button.TextButton"
- OutlinedButton:
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
OLD Support Library:
有了新的Support Library 28.0.0,设计库现在包含MaterialButton
.
您可以通过以下方式将此按钮添加到布局文件:
<android.support.design.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="YOUR TEXT"
android:textSize="18sp"
app:icon="@drawable/ic_android_white_24dp" />
默认情况下,这个类将使用主题的accent colour作为按钮的填充背景 colored颜色 ,同时使用白色作为按钮的文本 colored颜色 .
您可以使用以下属性自定义按钮:
app:rippleColor
: 要用于按钮涟漪效果的 colored颜色
app:backgroundTint
: 用于将色调应用于按钮的背景.如果要更改按钮的背景色,请使用此属性而不是背景.
app:strokeColor
:用于按钮笔划的 colored颜色
app:strokeWidth
: 用于按钮笔划的宽度
app:cornerRadius
: ,用于定义按钮拐角使用的半径