Android中tablayout怎么自定义布局


要自定义TabLayout的布局,可以通过实现自定义的TabLayout.TabLayout中的Tab的自定义布局。

首先,在xml文件中定义自定义的Tab布局,例如:

<LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><ImageViewandroid:id="@+id/tab_icon"android:layout_width="wrap_content"android:layout_height="wrap_content"/><TextViewandroid: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的时候,可以根据需要设置不同的自定义布局。


上一篇:java中怎么用gzip压缩多个文件

下一篇:c语言清空数组元素的方法是什么


android
Copyright © 2002-2019 测速网 https://www.inhv.cn/ 皖ICP备2023010105号 城市 地区 街道
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!
热门搜索