Material Design Components之MaterialButton的使用

一:導(dǎo)包

implementation 'com.google.android.material:material:1.0.0'

二:App主題

當(dāng)APP的主題是Theme.AppCompat系列時是不能使用com.google.android.material.button.MaterialButton控件的,否則匯報一個異常:

 Caused by: java.lang.IllegalArgumentException: This component requires that you specify a valid TextAppearance attribute. Update your app theme to inherit from Theme.MaterialComponents (or a descendant).
        at com.google.android.material.internal.ThemeEnforcement.checkTextAppearance(ThemeEnforcement.java:170)

意思是APP的主題只能使用繼承自Theme.MaterialComponents系列的,其實給對應(yīng)的Activity添加也是沒有問題的。

android:theme="@style/Theme.MaterialComponents.Light"

三:使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.google.android.material.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Default" />

    <com.google.android.material.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:enabled="false"
        android:text="Disable" />

    <com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="UnelevatedButton" />

    <com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="OutlinedButton" />

    <com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.TextButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextButton" />

    <com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.Icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="IconButton"
        app:icon="@drawable/ic_camera" />

</LinearLayout>
MaterialButton.png
  • 其中按鈕的填充色是由<item name="colorPrimary">@color/colorPrimary</item>決定的,如果同時給定了<item name="colorAccent">@color/colorAccent</item>那么按鈕的填充色會是colorAccent;對于沒有填充色的按鈕,colorPrimary決定了他的文字顏色,如果同時給定了colorAccent顏色,那么文字的顏色會是colorAccent。
  • 按鈕按下周圍會有一圈的陰影,該陰影延伸到了按鈕的邊界之外,所以包裹按鈕的ViewGroup應(yīng)該設(shè)置android:clipToPadding=“false”,以防按鈕陰影被父邊界剪裁掉。

四:樣式

一:默認(rèn)樣式style="@style/Widget.MaterialComponents.Button":有填充色、有陰影;

<com.google.android.material.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Default" />

    <com.google.android.material.button.MaterialButton
        android:layout_width="wrap_content"
        style="@style/Widget.MaterialComponents.Button"
        android:layout_height="wrap_content"
        android:text="Default" />

二:style="@style/Widget.MaterialComponents.Button.UnelevatedButton":有填充色、沒有陰影;

<com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="UnelevatedButton" />

三:style="@style/Widget.MaterialComponents.Button.OutlinedButton":透明背景、彩色文字、有輪廓,沒有陰影;

<com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="OutlinedButton" />

四:style="@style/Widget.MaterialComponents.Button.TextButton":透明背景、彩色文字、沒有輪廓,沒有陰影;

<com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.TextButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextButton" />

五:style="@style/Widget.MaterialComponents.Button.Icon":有填充色、有陰影、有一個小圖標(biāo);

<com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.Icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="IconButton"
        app:icon="@drawable/ic_camera" />

五:特有屬性

<com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.Icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Icon Button"
        app:icon="@drawable/ic_camera"
        app:iconGravity="textStart"
        app:iconSize="24dp"
        app:iconPadding="16dp"
        app:cornerRadius="40dp"
        app:iconTint="#0F0"
        app:strokeColor="#0F0"
        app:strokeWidth="2dp"
        app:rippleColor="#00F"/>
  • app:icon="@drawable/ic_camera" 圖標(biāo)
  • app:iconGravity="textStart" 圖標(biāo)的位置
  • app:iconSize="24dp" 圖標(biāo)的大小
  • app:iconPadding="16dp" 圖標(biāo)與文字的距離
  • app:cornerRadius="40dp" 按鈕圓角半徑
  • app:iconTint="#0F0" 圖標(biāo)著色
  • app:strokeColor="#0F0" 輪廓的顏色
  • app:strokeWidth="2dp" 輪廓的線寬
  • app:rippleColor="#00F" 按壓水波紋的顏色


    button.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容