LoginSignup
3
1

More than 5 years have passed since last update.

ToolbarにTabLayoutをもたせてみる

Posted at

ToolbarはViewを内部に持つことが可能
なので

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    >
    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="64dp"
        android:layout_marginRight="64dp"
        android:background="@android:color/background_light"
        app:tabGravity="fill"
        app:tabMode="scrollable"
        app:tabIndicatorColor="@color/colorPrimary"
        app:tabIndicatorHeight="2dp"
        />
</androidx.appcompat.widget.Toolbar>

のようにしてToolbarの中にTabを作る事もできる。

device-2018-11-29-201347.png

両サイドに透過グラデーションをつけたdrawableを背景にしたViewを配置して、すみっこに行った時に消えるような感じ

<androidx.appcompat.widget.Toolbar
     android:id="@+id/toolbar"
     android:layout_width="match_parent"
     android:layout_height="?attr/actionBarSize"
     android:background="@color/colorPrimary"
     >
     <FrameLayout
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:layout_marginLeft="64dp"
         android:layout_marginRight="64dp"
         >
         <com.google.android.material.tabs.TabLayout
             android:id="@+id/tab_layout"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:layout_gravity="center_horizontal"
             app:tabGravity="fill"
             app:tabMode="scrollable"
             app:tabIndicatorColor="@color/colorPrimary"
             app:tabIndicatorHeight="2dp"
             android:background="@android:color/background_light"
             />
         <View
             android:layout_width="52dp"
             android:layout_height="match_parent"
             android:background="@drawable/gradation_left"
             android:layout_gravity="start"
             />
         <View
             android:layout_width="52dp"
             android:layout_height="match_parent"
             android:background="@drawable/gradation_right"
             android:layout_gravity="end"
             />
     </FrameLayout>
  </androidx.appcompat.widget.Toolbar>

device-2018-11-29-202111.png

やってみたけどToolbarの中でやる必要は無いと思う。FrameLayoutで重ねても出来るし。
NavigationDrawerとかCollapsingToolbarLayoutを使っている時に使えるかもしれない?いやないか

3
1
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
3
1