嗨,我面临同样的问题,这是我能找到的最好的解决方案:

您应该使用(setCustomView),首先创建一个新的布局文件

让它命名(customtab.xml):

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:scaleType="fitCenter"

android:id="@+id/icon"

android:layout_gravity="center_horizontal" />

然后为每个选项卡执行此操作:(使用相同的布局.xml)

View view1 = getLayoutInflater().inflate(R.layout.customtab, null);

view1.findViewById(R.id.icon).setBackgroundResource(R.drawable.my1);

tabLayout.addTab(tabLayout.newTab().setCustomView(view1));

View view2 = getLayoutInflater().inflate(R.layout.customtab, null);

view2.findViewById(R.id.icon).setBackgroundResource(R.drawable.my2);

tabLayout.addTab(tabLayout.newTab().setCustomView(view2));

View view3 = getLayoutInflater().inflate(R.layout.customtab, null);

view3.findViewById(R.id.icon).setBackgroundResource(R.drawable.my3);

tabLayout.addTab(tabLayout.newTab().setCustomView(view3));

...

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐