Material Design Components之MaterialButton的使用

一:導包

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

二:App主題

當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系列的,其實給對應的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應該設置android:clipToPadding=“false”,以防按鈕陰影被父邊界剪裁掉。

四:樣式

一:默認樣式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":有填充色、有陰影、有一個小圖標;

<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" 圖標
  • app:iconGravity="textStart" 圖標的位置
  • app:iconSize="24dp" 圖標的大小
  • app:iconPadding="16dp" 圖標與文字的距離
  • app:cornerRadius="40dp" 按鈕圓角半徑
  • app:iconTint="#0F0" 圖標著色
  • 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ā)布平臺,僅提供信息存儲服務。

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

  • 眾所周知Material Design(材質(zhì)設計)是Google在2014年I/O大會上發(fā)布的一種新的設計規(guī)范。一...
    _SOLID閱讀 57,476評論 49 324
  • 前言:近來學習了Android Material Design 兼容庫,為了把這個弄懂,才有了這篇博客,這里先推薦...
    mecury閱讀 5,761評論 3 21
  • Material Design常用的主題 Api >= 21@android:style/Theme.Materi...
    王志強_9380閱讀 673評論 0 0
  • MaterialDesign是一套全新的界面設計語言,包含視覺,運動,互動效果等特性. Design Suppor...
    小徐andorid閱讀 1,116評論 0 9
  • 2019雜感《玄幻》,2019年我決定不再在朋友圈曬讀書了(我一直以為朋友圈曬讀書跟土豪曬金鏈子炫富沒太大區(qū)別[偷...
    地中海的風_51b9閱讀 596評論 0 2

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