概要
Androidプロジェクトにマテリアルデザインを導入後にやることを書きます。
Androidプロジェクト作成した後のHello worldに対して行います。
ライブラリ導入
マテリアルデザインを導入するために、build.gradle
を以下のように記載します。
build.gradle
// material design
api 'com.google.android.material:material:1.2.0-alpha06'
styles.xmlの変更
res/values/styles.xml
を以下のように記載します。
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
activity_main.xmlの変更
res/layout/activity_main.xml
を変更して表示を確認します。
<?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.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="確認"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
結果
余談
res/values/styles.xml
を初期値の状態のままだと以下のエラーが表示されます。
Error inflating class com.google.android.material.button.MaterialButton
参考
- Developer tutorials
- Getting started with Material Components for Android