Home > Net >  Kotlin: How to differentiate TabItem Width in TabLayout?
Kotlin: How to differentiate TabItem Width in TabLayout?

Time:01-08

I have three taps with below code.

<com.google.android.material.tabs.TabLayout
            android:id="@ id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_alignParentBottom="true"
            android:elevation="5dp"
            app:tabMaxWidth="0dp"
            app:tabGravity="fill"
            app:tabIndicatorColor="@color/main_color">

            <com.google.android.material.tabs.TabItem
                android:id="@ id/myDiaryTab"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="내 일기" />
            
            <com.google.android.material.tabs.TabItem
                android:id="@ id/allDiaryTab"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="우리 일기" />


            <com.google.android.material.tabs.TabItem
                android:id="@ id/moreTab"
                android:layout_width="20dp"
                android:layout_height="wrap_content"
                android:icon="@drawable/ic_hamburger" />

        </com.google.android.material.tabs.TabLayout>

And it shows below tab menu.

enter image description here

I want third menu of hamburger icon have shorter width than others.

But TabItem width is only applied icon I guess.

I can't use width and weight.

How can I do this?

CodePudding user response:

Layoutparams.weight will work for the item.

val yourTabLayoutView = binding.tabLayout // If you aren't using data biniding, You can use findViewById to get the view
val yourTabItemView = ((yourTabLayoutView.getChildAt(0) as LinearLayout).getChildAt(2) // 2 is for your tab item position.
yourTabItemView.layoutParams as LinearLayout.LayoutParams).weight = 0.2f // you can set the weight at here. Current one is 0.2, which means 20% size of the tab layout width.
  • Related