如何更改默认的tabicon位置?

时间:2018-04-28 19:19:47

标签: android android-fragments android-tablayout

我已添加片段标题和图标,如下所示。默认情况下,选项卡图标位于上方。我想将这些图标保留在左侧。是否可以在以下结构中更改位置,还是需要自定义代码?

    // Add Fragments to adapter one by one
    adapter.addFragment(new Home_Screen(), "Home");
    adapter.addFragment(new YebformCover(), "Yeb");
    adapter.addFragment(new ServiceCover(), "Service");
    adapter.addFragment(new FeaturedCover(), "Feature");
    viewPager.setAdapter(adapter);

    TabLayout tabLayout = findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    tabLayout.getTabAt(1).setIcon(R.drawable.ic_action_home);
    tabLayout.getTabAt(2).setIcon(R.drawable.ic_action_school);
    tabLayout.getTabAt(3).setIcon(R.drawable.ic_action_development);
    tabLayout.getTabAt(4).setIcon(R.drawable.ic_action_plan);

1 个答案:

答案 0 :(得分:0)

您需要将customView设置为标签

查看setCustomView(View),您需要应用于标签页。

创建自定义布局文件,使用LayoutInflater对布局进行充气并将其设置为标签 -

TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabOne.setText("Tab 1");
tabOne.setCompoundDrawablesWithIntrinsicBounds(R.drawable.your_icon, 0, 0, 0);
// Set layout to first tab
tabLayout.getTabAt(0).setCustomView(tabOne);