溫馨提示×

Android中tablayout怎么自定義布局

小億
465
2024-03-06 17:01:46
欄目: 編程語言

要自定義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的時候,可以根據需要設置不同的自定義布局。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女