如何在TabLayout中减少标签图标和标签文本之间的高度

时间:2018-06-20 09:40:51

标签: android android-tablayout

我正在使用标签布局,在其中我以编程方式添加标签图标和标签文本,并在addOnTabSelectedListener上更改了图标和文本颜色,这非常正常。

我的问题是我想减少标签图标和标签文本之间的高度。我不能那样做。有人可以帮我解决这个问题。

修改 如何通过使用自定义标签布局实现此目标

这是我的截图

Here is my screenshot

这是TabLayout

<android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            style="@style/MyStyle"
            android:background="@drawable/tab_selector"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            app:tabTextAppearance="@style/MineCustomTabText"
            app:tabGravity="fill"
            app:tabMode="fixed"
            app:tabTextColor="#000" />

这是我的代码

public class MainActivity extends AppCompatActivity {

    TabLayout tabLayout;
    ViewPager viewPager;
    ViewPagerAdapter viewPagerAdapter;
    final int[] ICONS = new int[]{
            R.drawable.ic_action_home,
            R.drawable.ic_action_me,
            R.drawable.ic_action_team,
            R.drawable.ic_action_company}; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        tabLayout = (TabLayout) findViewById(R.id.tabs);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(viewPagerAdapter);
        tabLayout.setupWithViewPager(viewPager); 

        int tabIconColor = ContextCompat.getColor(getApplicationContext(), R.color.tabSelectedIconColor);
        tabLayout.getTabAt(0).setIcon(ICONS[0]).getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);;
        tabLayout.getTabAt(1).setIcon(ICONS[1]);
        tabLayout.getTabAt(2).setIcon(ICONS[2]);
        tabLayout.getTabAt(3).setIcon(ICONS[3]);


        tabLayout.addOnTabSelectedListener (
                new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
                    @Override
                    public void onTabSelected(TabLayout.Tab tab) {
                        super.onTabSelected(tab);
                        int tabIconColor = ContextCompat.getColor(getApplicationContext(), R.color.tabSelectedIconColor);
                        tab.getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);
                    }

                    @Override
                    public void onTabUnselected(TabLayout.Tab tab) {
                        super.onTabUnselected(tab);
                        int tabIconColor = ContextCompat.getColor(getApplicationContext(), R.color.tabUnselectedIconColor);
                        tab.getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);
                    }

                    @Override
                    public void onTabReselected(TabLayout.Tab tab) {
                        super.onTabReselected(tab);
                    }
                }
        ); 

    } 

}

0 个答案:

没有答案