본문 바로가기
프로그래밍/Android, iOS

[Android] Toolbar padding in Tablet

by YuminK 2022. 3. 30.

stackover flow를 찾아보니까, Toolbar를 찾아와서 거기에 직접 자식을 넣고 inflate하는 방식, 속성 처리..

여러 방법이 있는데 나의 경우에는 다른 문제였다.

찾아보면 이런 거 많이 한다.

나는 Material Design을 이용해서 AppBarLayout, MaterialToolbar를 이용하고 내부에 ConstraintLayout을 사용하여 구현했다.

이런 상황에서 Tablet기기부터 좌우에 뭔가 색상이 뭍어나오는데 Tablet보다 작은 크기의 기기들은 문제가 생기지 않았다.

 <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentInsetEnd="0dp"
            app:contentInsetStart="0dp">

contentInsetStart, consentInsetEnd값을 주지 않으면 옆에 공간이 남는다.

추가적으로 paddingStart, paddingEnd에 값을 줘야 한다.

안드로이드 내부에서는 기기의 크기에 따라서 Toolbar에 자동으로 padding값을 주는 것으로 보인다.

그러니까, Tablet기기부터 문제가 생긴 것이다.

Padding값을 적용하니 문제가 사라진 모습이다.

전체 코드

<?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.appbar.AppBarLayout
        android:id="@+id/appbar_layout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            android:paddingStart="0dp"
            android:paddingEnd="0dp"
            app:contentInsetEnd="0dp"
            app:contentInsetStart="0dp">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/toolbar_inner_layout"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@android:color/darker_gray">

                <ImageView
                    android:id="@+id/menu_image_view"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:layout_gravity="center_vertical"
                    android:layout_marginStart="16dp"
                    android:src="@drawable/ic_android_black_24dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <ImageView
                    android:id="@+id/uprism_logo_image_view"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:layout_gravity="center"
                    android:src="@drawable/ic_android_black_24dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

            </androidx.constraintlayout.widget.ConstraintLayout>

        </com.google.android.material.appbar.MaterialToolbar>

    </com.google.android.material.appbar.AppBarLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

댓글