要实现Android TabLayout支持多行文本描述,可以使用自定义布局和自定义视图的方式来实现。以下是一个示例代码:
public class TabAdapter extends TabLayout.TabAdapter {
private final Context context;
private final List titles;
public TabAdapter(Context context, List titles) {
this.context = context;
this.titles = titles;
}
@Override
public View getView(TabLayout.Tab tab) {
View view = LayoutInflater.from(context).inflate(R.layout.tab_layout_item, null);
ImageView tabIcon = view.findViewById(R.id.tab_icon);
TextView tabTitle = view.findViewById(R.id.tab_title);
tabIcon.setImageResource(R.drawable.ic_tab_icon);
tabTitle.setText(titles.get(tab.getPosition()));
return view;
}
}
TabLayout tabLayout = findViewById(R.id.tab_layout);
ViewPager viewPager = findViewById(R.id.view_pager);
List titles = new ArrayList<>();
titles.add("Tab 1");
titles.add("Tab 2");
titles.add("Tab 3");
TabAdapter tabAdapter = new TabAdapter(this, titles);
tabLayout.setTabAdapter(tabAdapter);
viewPager.setAdapter(new PagerAdapter(getSupportFragmentManager()));
tabLayout.setupWithViewPager(viewPager);
通过以上步骤,就可以实现Android TabLayout支持多行文本描述的效果了。