删除TabLayout左右填充

Nit*_*rma 1 android android-tablayout

我正在使用标签布局,这是我的代码

  <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="center"
        app:tabMode="fixed"

        android:minHeight="?attr/actionBarSize"
        app:tabTextColor="#000"
        app:tabSelectedTextColor="#fff"
        app:tabIndicatorColor="@android:color/white"

        android:clipToPadding="false"
        />
Run Code Online (Sandbox Code Playgroud)

像这样添加标签

//创建标签

TextView tab =(TextView)LayoutInflater.from(this).inflate(R.layout.custom_tab,null); tab.setText( "家"); tab.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.ic_home_black_24dp,0,0); tabLayout.addTab(tabLayout.newTab()setCustomView(标签).);

TextView tab2 = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tab2.setText("Report");
tab2.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.ic_trending_up_black_24dp, 0, 0);
tabLayout.addTab(tabLayout.newTab().setCustomView(tab2));

TextView tab3 = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tab3.setText("Medicine");
tab3.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.ic_home_black_24dp, 0, 0);
tabLayout.addTab(tabLayout.newTab().setCustomView(tab3));

TextView tab4 = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tab4.setText("More");
tab4.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.ic_trending_up_black_24dp, 0, 0);
tabLayout.addTab(tabLayout.newTab().setCustomView(tab4));
Run Code Online (Sandbox Code Playgroud)

这是我的手机截图

https://i.stack.imgur.com/kYNs1.png
Run Code Online (Sandbox Code Playgroud)

两件事1.如何从标签布局的左右两侧移除空间?

2.如何更改活动和非活动选项卡的文本颜色和图标

Aks*_*kar 8

  1. 从选项卡布局中的选项卡中删除填充

    在TabLayout中,您必须将tabPaddingEnd和tabPaddingStart设置为0dp.

    <android.support.design.widget.TabLayout
    app:tabPaddingStart="0dp"
    app:tabPaddingEnd="0dp"/>
    
    Run Code Online (Sandbox Code Playgroud)
  2. 更改TabLayout所选选项卡图标的颜色

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);
    
    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    tabLayout.setOnTabSelectedListener(
            new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
    
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    super.onTabSelected(tab);
                    int tabIconColor = ContextCompat.getColor(context, 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(context, R.color.tabUnselectedIconColor);
                    tab.getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);
                }
    
                @Override
                public void onTabReselected(TabLayout.Tab tab) {
                    super.onTabReselected(tab);
                }
            }
    );
    
    Run Code Online (Sandbox Code Playgroud)