要自定义TabLayout的布局,可以通过实现自定义的TabLayout.TabLayout中的Tab的自定义布局。
首先,在xml文件中定义自定义的Tab布局,例如:
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:id="@+id/tab_icon" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/tab_text" android:layout_width="wrap_content" android:layout_height="wrap_content"/></LinearLayout>然后,在代码中创建自定义的TabLayout.Tab,并设置自定义的布局:
TabLayout.Tab tab1 = tabLayout.newTab();View customTab1 = LayoutInflater.from(this).inflate(R.layout.custom_tab_layout, null);ImageView tabIcon1 = customTab1.findViewById(R.id.tab_icon);TextView tabText1 = customTab1.findViewById(R.id.tab_text);tabIcon1.setImageResource(R.drawable.tab_icon);tabText1.setText("Tab 1");tab1.setCustomView(customTab1);tabLayout.addTab(tab1);这样就可以实现自定义TabLayout的布局。在创建Tab的时候,可以根据需要设置不同的自定义布局。




