最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android?MaterialButton使用實(shí)例詳解(告別shape、selector)

 更新時(shí)間:2022年09月03日 08:35:43   作者:yechaoa  
我們平時(shí)寫(xiě)布局,當(dāng)遇到按鈕需要圓角、或者描邊等,通常的方法是新建一個(gè)xml文件,在shape標(biāo)簽下寫(xiě),然后通過(guò)android:background或setBackground(drawable)設(shè)置,這篇文章主要給大家介紹了關(guān)于Android?MaterialButton使用詳解的相關(guān)資料,需要的朋友可以參考下

效果

前言

先來(lái)看一下MaterialButton是什么

由上圖可以看到MaterialButton也沒(méi)有什么神秘的,不過(guò)是Button的一個(gè)子類(lèi)而已,但是經(jīng)過(guò)谷歌的封裝之后,在符合Material Design的基礎(chǔ)上,使用起來(lái)更加方便了,且容易實(shí)現(xiàn)預(yù)期效果。

使用

引入material包

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

常規(guī)

<com.google.android.material.button.MaterialButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/app_name"
    android:textAllCaps="false" />

與Button使用無(wú)異,textAllCaps是取消全部大小寫(xiě)。

圖標(biāo)

<com.google.android.material.button.MaterialButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:text="@string/app_name"
    android:textAllCaps="false"
    app:icon="@mipmap/ic_launcher" />

app:icon屬性指定圖標(biāo)。

圓角

<com.google.android.material.button.MaterialButton
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:text="@string/app_name"
    android:textAllCaps="false"
    app:cornerRadius="25dp"
    app:icon="@mipmap/ic_launcher"
    app:iconGravity="end" />

app:cornerRadius屬性指定圓角大小。

Button描邊

<com.google.android.material.button.MaterialButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:text="@string/app_name"
    android:textAllCaps="false"
    app:cornerRadius="25dp"
    app:strokeColor="@color/black"
    app:strokeWidth="3dp" />
  • app:strokeColor 描邊顏色
  • app:strokeWidth 描邊寬度

文字描邊

<com.google.android.material.button.MaterialButton
    style="@style/Widget.MaterialComponents.Button.OutlinedButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:text="@string/app_name"
    android:textAllCaps="false"
    app:cornerRadius="5dp"
    app:rippleColor="@color/red"
    app:strokeColor="@color/red"
    app:strokeWidth="3dp" />
  • 與上面不同的是,這里用了style,區(qū)別在于上面的是常規(guī)Button狀態(tài)下的描邊,這個(gè)是文字Button狀態(tài)下的描邊。
  • app:rippleColor 點(diǎn)擊波紋顏色

文字按鈕

<com.google.android.material.button.MaterialButton
    style="@style/Widget.MaterialComponents.Button.TextButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:text="@string/app_name"
    android:textAllCaps="false" />

與常規(guī)使用方法一樣,但是加了style,這個(gè)style在未選中的情況下,對(duì)背景色設(shè)置了透明。

圓形Button

<com.google.android.material.button.MaterialButton
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_marginTop="20dp"
    android:insetTop="0dp"
    android:insetBottom="0dp"
    android:text="@string/login"
    android:textAllCaps="false"
    android:textSize="20sp"
    app:cornerRadius="999dp"
    app:strokeColor="@color/orange"
    app:strokeWidth="5dp" />

這里為什么來(lái)個(gè)圓形Button呢,是因?yàn)樯婕暗絻蓚€(gè)屬性

  • android:insetTop 上邊距
  • android:insetBottom 下邊距

這兩個(gè)參數(shù)默認(rèn)是6dp,不設(shè)置為0dp的話,就不是一個(gè)規(guī)則的圓。

關(guān)于其他屬性的默認(rèn)參數(shù),可以在xml文件的右上角,選中Design面板,選擇要查看的View即可。

源碼分析icon

唯一不足的是MaterialButton不能像chip一樣給icon設(shè)置事件。

來(lái)看看源碼中 icon具體是什么實(shí)現(xiàn)的:

  public void setIcon(@Nullable Drawable icon) {
    if (this.icon != icon) {
      this.icon = icon;
      updateIcon(/* needsIconUpdate = */ true);
    }
  }

這里比較簡(jiǎn)單,繼續(xù)看調(diào)用的updateIcon方法

  private void updateIcon(boolean needsIconUpdate) {
    // Forced icon update
    if (needsIconUpdate) {
      resetIconDrawable(isIconStart);
      return;
    }
	...
    if (hasIconChanged) {
      resetIconDrawable(isIconStart);
    }
  }

有省略,看關(guān)鍵兩段代碼都調(diào)用了resetIconDrawable方法,繼續(xù)

  private void resetIconDrawable(boolean isIconStart) {
    if (isIconStart) {
      TextViewCompat.setCompoundDrawablesRelative(this, icon, null, null, null);
    } else {
      TextViewCompat.setCompoundDrawablesRelative(this, null, null, icon, null);
    }
  }

相信到這里很多人就明白了,icon的實(shí)現(xiàn)等同于drawableStart。

只不過(guò)在MaterialButton中drawableStart是沒(méi)有效果的,而是iconiconGravity配合使用來(lái)達(dá)到效果。

屬性

關(guān)于xml屬性,我做了一個(gè)整理

屬性含義
insetBottom下邊距,默認(rèn)6dp
insetTop上邊距,默認(rèn)6dp
cornerRadius圓角大小
icon圖標(biāo)
iconGravity圖標(biāo)位置,只能前后
iconPadding圖標(biāo)距文字距離,默認(rèn)8dp
iconSize圖標(biāo)大小
iconTint圖標(biāo)著色
iconTintMode圖標(biāo)著色模式
rippleColor點(diǎn)擊波紋顏色
strokeColor描邊顏色
strokeWidth描邊寬度
app:backgroundTint背景色(注意命名空間)

Github

https://github.com/yechaoa/MaterialDesign

感謝

官方文檔

最后

到此這篇關(guān)于Android MaterialButton使用實(shí)例詳解的文章就介紹到這了,更多相關(guān)Android MaterialButton使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

汶上县| 绥宁县| 东兰县| 华池县| 汶上县| 安徽省| 郴州市| 垦利县| 通化县| 吕梁市| 囊谦县| 淮阳县| 东海县| 垦利县| 太仆寺旗| 雅安市| 临沭县| 濮阳市| 安化县| 祁门县| 二连浩特市| 甘泉县| 伊春市| 盘山县| 凤凰县| 疏勒县| 凯里市| 古交市| 昆山市| 商水县| 汶川县| 东阿县| 卢龙县| 肥东县| 丰城市| 武汉市| 顺义区| 龙江县| 永清县| 乌兰察布市| 汉寿县|