LoginSignup
0
0

More than 3 years have passed since last update.

Android Toolbarの書き方(標準、カスタム)

Last updated at Posted at 2019-08-30

発信したいこと

久々の投稿でございます.

この記事を書くに至ったのは複数のAndroidプロジェクトを見ていて
Toolbarの実装方法が異なりどちらが実装しやすいのかなと思い投稿させていただきました。
やり方は様々あるのであくまで一つのやり方として参考にしてもらえれば幸いです!

環境

Mac : 10.14.3
AndroidStudio : 3.4.2
Language : Kotlin

実装

カスタムのxml

Toolbarの中にゴリゴリ、レイアウトを実装しています.

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar1"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary">

    <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <ImageView
                android:id="@+id/navigationImageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_arrow_back_24dp"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                tools:ignore="ContentDescription"/>

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/title"
                android:layout_marginStart="28dp"
                android:textSize="20sp"
                android:textColor="@color/white"
                app:layout_constraintTop_toTopOf="@+id/navigationImageView"
                app:layout_constraintBottom_toBottomOf="@+id/navigationImageView"
                app:layout_constraintStart_toEndOf="@+id/navigationImageView"/>

        <ImageView
                android:id="@+id/menuImageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_settings_24dp"
                android:layout_marginEnd="20dp"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                tools:ignore="ContentDescription"/>

    </android.support.constraint.ConstraintLayout>

</android.support.v7.widget.Toolbar>

標準のxml

スッキリして見やすいですね!

<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar2"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimaryDark"
            app:title="@string/title"
            app:titleTextColor="@color/white"
            app:navigationIcon="@drawable/ic_arrow_back_24dp"/>

所感

カスタム

  • メリット
    自由自在にレイアウトできる。 iOSと同じようにして欲しい要望に答えることができる
  • デメリット
    複数エンジニアが開発している場合、全画面のToolbarを同様にすることが難しい

標準

  • メリット
    コードを初見で理解できる
  • デメリット
    特に見当たらない

コード見たい人はココ

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0