AndroidのTabLayoutでカスタムレイアウトを作成する方法は何ですか?
TabLayoutのレイアウトをカスタマイズするには、TabLayout.TabLayout内のタブのカスタムレイアウトを実装することができます。
最初に、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を作成する際に、必要に応じて異なるカスタムレイアウトを設定することができます。