Android 配置Material Design庫及使用MaterialButton

Android 配置Material Design庫

1.首先在gradle.build文件中添加依賴

implementation 'com.android.support:design:29.0.2'

添加後的gradle文件如下,多的可刪可不刪

ependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'com.android.support:design:29.0.2'
}

2.更換AndroidManifest文件中的Theme,也就是value/style中的AppTheme屬性,如果不修改這個,使用的組件就只有最基本的效果,比如MaterialButton就沒有水波紋的效果

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">

3.在需要使用Material組件的layout文件中添加

xmlns:app="http://schemas.android.com/apk/res-auto"

如果有的話就不用加了

4.最後給出一個MaterialButton的代碼,如果能跑起來並切有水波紋效果就說明配置成功了

    <com.google.android.material.button.MaterialButton
        android:layout_width="200dp"
        android:layout_height="60dp"
        android:gravity="center"
        android:text="MaterialButton"
        android:textColor="#ffffffff"
        android:textSize="24sp"
        app:backgroundTint="#FFA54C" />

5.配置完以後,如果寫代碼沒有自動補全什麼的,可以試着Build 一> Rebuild Project一下

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章