<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Material Components之旅——MaterialButton

          共 6848字,需瀏覽 14分鐘

           ·

          2021-04-07 10:22

          MaterialButton相比我們前面介紹的ShapeableImageView等組件來(lái)說(shuō),用的相對(duì)較少一些,但是在某些場(chǎng)景下,還是很有用的,多個(gè)朋友多條路,所以多掌握一些總是沒(méi)錯(cuò)的。

          主題的遷移

          MaterialButton的使用,需要使用到最新的Theme.MaterialComponents主題,否則會(huì)報(bào)錯(cuò),所以在使用前,需要進(jìn)行Theme的升級(jí),官方提供了非常詳細(xì)的說(shuō)明文檔,可以讓大家非常方便的進(jìn)行遷移,甚至還提供了用于過(guò)渡的bridge版本,具體的說(shuō)明文檔,大家可以參考下面的官方鏈接。

          https://github.com/material-components/material-components-android/blob/master/docs/getting-started.md

          https://github.com/material-components/material-components-android/blob/master/docs/components/Button.md

          對(duì)于MaterialButton來(lái)說(shuō),不侵入項(xiàng)目的最簡(jiǎn)單的方式,就是給button設(shè)置Theme,代碼如下所示。

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

          初步

          MaterialButton繼承AppCompatButton,在原有Button的基礎(chǔ)上進(jìn)行了擴(kuò)展,例如圓角、描邊、內(nèi)置icon(icon支持設(shè)置Size、Tint、Padding、Gravity等之前Button不支持的屬性),同時(shí)也支持了MD的一些屬性,例如支持按壓水波紋并且設(shè)置color等。

          公開(kāi)屬性如下所示:

          屬性描述
          app:backgroundTint背景著色
          app:backgroundTintMode背景著色模式
          app:strokeColor描邊顏色
          app:strokeWidth描邊寬度
          app:cornerRadius圓角大小
          app:rippleColor水波紋顏色
          app:icon內(nèi)置icon
          app:iconSize內(nèi)置icon大小
          app:iconGravity內(nèi)置icon位置
          app:iconTint內(nèi)置icon著色
          app:iconTintMode內(nèi)置icon著色模式
          app:iconPadding內(nèi)置icon和文本之間的間距

          基本上都是對(duì)Button的屬性補(bǔ)充。

          background

          MaterialButton的背景,可以支持設(shè)置純色和其它Drawable兩種方式。

          純色背景色通過(guò)backgroundTint設(shè)置,代碼如下所示。

          <com.google.android.material.button.MaterialButton
          ????style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
          ????android:layout_width="200dp"
          ????android:layout_height="100dp"
          ????android:insetTop="0dp"
          ????android:insetBottom="0dp"
          ????android:text="Button"
          ????android:theme="@style/Theme.MaterialComponents.Light.NoActionBar.Bridge"
          ????app:backgroundTint="#bebebe"
          ????app:layout_constraintBottom_toBottomOf="parent"
          ????app:layout_constraintEnd_toEndOf="parent"
          ????app:layout_constraintStart_toStartOf="parent"
          ????app:layout_constraintTop_toTopOf="parent"
          ????app:rippleColor="@null"?/>
          da3abf80fd119578ec0ff3eb4cbdf372.webpimage-20210329151657903

          而如果是漸變等Drawable,那就需要自己創(chuàng)建Drawable,代碼如下所示。

          <com.google.android.material.button.MaterialButton
          ????style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
          ????android:layout_width="200dp"
          ????android:layout_height="100dp"
          ????android:background="@drawable/bg_gradient"
          ????android:insetTop="0dp"
          ????android:insetBottom="0dp"
          ????android:text="Button"
          ????android:theme="@style/Theme.MaterialComponents.Light.NoActionBar.Bridge"
          ????app:backgroundTint="@null"
          ????app:layout_constraintBottom_toBottomOf="parent"
          ????app:layout_constraintEnd_toEndOf="parent"
          ????app:layout_constraintStart_toStartOf="parent"
          ????app:layout_constraintTop_toTopOf="parent"
          ????app:rippleColor="@null"?/>

          最重要的是,要設(shè)置backgroundTint="@null",同時(shí),由于設(shè)置了background,會(huì)導(dǎo)致圓角失效。

          默認(rèn)Padding

          MaterialButton的上下邊,默認(rèn)有6dp邊距,這是MD的設(shè)計(jì)規(guī)范,可以通過(guò)insetxxx來(lái)進(jìn)行修改,代碼如下所示。

          android:insetTop="0dp"
          android:insetBottom="0dp"
          0cdf63672efca769c0e234d8900bb4e9.webpimage-20210329151737200

          這個(gè)新的名詞——inset,可以通過(guò)這張圖了解。

          dee075861012d13cac615bd6352f8dbb.webpimg

          相信通過(guò)這張圖,大家可以很清楚的了解什么是inset了。

          MaterialButton Style

          MaterialButton可以設(shè)置幾種Style。

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

          • style="@style/Widget.MaterialComponents.Button.UnelevatedButton":有填充色、沒(méi)有陰影

          • style="@style/Widget.MaterialComponents.Button.OutlinedButton":透明背景、彩色文字、有輪廓,沒(méi)有陰影

          • style="@style/Widget.MaterialComponents.Button.TextButton":透明背景、彩色文字、沒(méi)有輪廓,沒(méi)有陰影

          • style="@style/Widget.MaterialComponents.Button.Icon":有填充色、有陰影、使用圖標(biāo)

          例如設(shè)置無(wú)陰影,可以使用下面的代碼。

          style="@style/Widget.MaterialComponents.Button.UnelevatedButton"

          MaterialButton Icon

          MaterialButton比Button更加靈活,它提供了更多的icon操作,代碼如下所示。

          app:icon="@drawable/abc_vector_test"
          8a8c9b643635231102ad700328b35143.webpimage-20210329153218740

          除此之外,還可以設(shè)置例如iconSize、iconGravity、iconPadding等屬性,這里就不一一演示了。

          shapeAppearance

          MaterialButton同樣可以通過(guò)shapeAppearance配合ShapeAppearanceModel來(lái)處理形狀,代碼如下所示。

          <com.google.android.material.button.MaterialButton
          ????style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
          ????android:layout_width="200dp"
          ????android:layout_height="100dp"
          ????android:insetTop="0dp"
          ????android:insetBottom="0dp"
          ????android:text="Button"
          ????android:theme="@style/Theme.MaterialComponents.Light.NoActionBar.Bridge"
          ????app:backgroundTint="#bebebe"
          ????app:layout_constraintBottom_toBottomOf="parent"
          ????app:layout_constraintEnd_toEndOf="parent"
          ????app:layout_constraintStart_toStartOf="parent"
          ????app:layout_constraintTop_toTopOf="parent"
          ????app:rippleColor="@null"
          ????app:shapeAppearance="@style/Cut"?/>
          ????
          ????
          <style?name="Cut"?parent="ShapeAppearance.MaterialComponents.SmallComponent">
          ????<item?name="cornerFamily">cut</item>
          ????<item?name="cornerSize">16dp</item>
          </style>
          dba3867a8f65d4a0ad85e1dd0113ea86.webpimage-20210329155533599

          MD系列組件的Shape,都可以使用shapeAppearance來(lái)實(shí)現(xiàn),大家需要觸類旁通。

          MaterialButtonToggleGroup

          MaterialButtonToggleGroup是類似iOS的Segment的一個(gè)新組件,用于取代Android之前的RadioGroup,使用方式也和RadioGroup非常類似,但是同樣的,提供了更多的MD設(shè)計(jì)元素屬性,下面這個(gè)例子演示了最基本的使用。

          <?xml?version="1.0"?encoding="utf-8"?>
          <androidx.constraintlayout.widget.ConstraintLayout?xmlns:android="http://schemas.android.com/apk/res/android"
          ????xmlns:app="http://schemas.android.com/apk/res-auto"
          ????xmlns:tools="http://schemas.android.com/tools"
          ????android:layout_width="match_parent"
          ????android:layout_height="match_parent"
          ????tools:context=".MainActivity">

          ????<com.google.android.material.button.MaterialButtonToggleGroup
          ????????android:layout_width="wrap_content"
          ????????android:layout_height="48dp"
          ????????app:checkedButton="@+id/button_wrap_mode_none"
          ????????app:layout_constraintBottom_toBottomOf="parent"
          ????????app:layout_constraintEnd_toEndOf="parent"
          ????????app:layout_constraintStart_toStartOf="parent"
          ????????app:layout_constraintTop_toTopOf="parent"
          ????????app:singleSelection="true">

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

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

          ????????<com.google.android.material.button.MaterialButton
          ????????????android:id="@+id/button_wrap_mode_align"
          ????????????style="@style/Widget.MaterialComponents.Button.OutlinedButton"
          ????????????android:layout_width="wrap_content"
          ????????????android:layout_height="wrap_content"
          ????????????android:text="Align"?/>
          ????</com.google.android.material.button.MaterialButtonToggleGroup>

          </androidx.constraintlayout.widget.ConstraintLayout>
          1c54c7ff9696d4f69de5a3b08039ed17.webpimage-20210329153549217

          和RadioGroup一樣,通過(guò)下面的代碼可以給MaterialButtonToggleGroup添加監(jiān)聽(tīng)。

          toggleGroup.addOnButtonCheckedListener?{?group,?checkedId,?isChecked?->?}

          MaterialButtonToggleGroup還有幾個(gè)主要的屬性:

          • singleSelection:是否單選
          • selectionRequired:是否必須要選中
          • checkedButton:默認(rèn)選中的ID
          • orientation:排列方式

          借助MaterialButtonToggleGroup和MaterialButton的配合使用,可以讓類似這種選擇器的使用變得更加簡(jiǎn)單。

          向大家推薦下我的網(wǎng)站?https://xuyisheng.top/??點(diǎn)擊原文一鍵直達(dá)

          專注 Android-Kotlin-Flutter 歡迎大家訪問(wèn)


          瀏覽 119
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国精产品高潮呻吟久久久… | 99久久这里只有精品 | 中国一级淫片 | 奇米无码在线 | 黑人大屌cao逼 |