2017-02-22 153 views
0

我使用的標籤佈局,這裏是我的代碼刪除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" 
     /> 

添加標籤這樣

//創建標籤

的TextView標籤=(TextView的)LayoutInflater。 from(this).inflate(R.layout.custom_tab,null); tab.setText(「Home」); tab.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.ic_home_black_24dp,0,0); tabLayout.addTab(tabLayout.newTab()。setCustomView(tab));

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)); 

,這裏是我的手機屏幕截圖

https://i.stack.imgur.com/kYNs1.png 

兩件事 我1.How可以去除左和標籤佈局的右側,其正在採取的空間?

我2.How可以更改活動和非活動標籤的文字和圖標的顏色

+0

使用app:tabPadding屬性 –

回答

5
  1. 以去除標籤填充在標籤佈局

    在你TabLayout你必須設置tabPaddingEnd和tabPaddingStart到0dp。通過

    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); 
          } 
         } 
    ); 
    
0

對於TabLayout的所選標籤的圖標的

<android.support.design.widget.TabLayout 
app:tabPaddingStart="0dp" 
app:tabPaddingEnd="0dp"/> 
  • 變色調整pading可以使用app:tabMinWidth像或tabPaddingStart/tabPaddingEnd/tabPaddingTop/tabPaddingBottom

    <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" 
         app:tabMinWidth="50dp" 
         app:tabPaddingBottom="1dp" 
         app:tabPaddingEnd="1dp" 
         app:tabPaddingStart="1dp" 
         app:tabPaddingTop="1dp" 
         android:minHeight="?attr/actionBarSize" 
         app:tabTextColor="#000" 
         app:tabSelectedTextColor="#fff" 
         app:tabIndicatorColor="@android:color/white" 
    
         android:clipToPadding="false" 
         /> 
    

    對於簡單地就可以使用下面的代碼顏色

    <?xml version="1.0" encoding="utf-8"?> 
    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
        <item android:drawable="@color/tab_background_selected" android:state_selected="true"/> 
        <item android:drawable="@color/tab_background_unselected"/> 
    </selector> 
    

    和它繪製保存爲tab_color_selector.xml

    ,並使用

    app:tabBackground="@drawable/tab_color_selector" 
    

    所以總的代碼將

    <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" 
         app:tabMinWidth="50dp" 
         android:minHeight="?attr/actionBarSize" 
         app:tabTextColor="#000" 
         app:tabSelectedTextColor="#fff" 
         app:tabIndicatorColor="@android:color/white" 
         app:tabBackground="@drawable/tab_color_selector" 
         android:clipToPadding="false" 
         /> 
    

    給顏色tab_background_selectedtab_background_unselected#000000。或使用#000000,而不是直接@color/tab_background_unselected

  • +0

    它從左右兩邊取空間 –

    0

    試試這個,

    <android.support.design.widget.TabLayout 
         android:id="@+id/tab_layout" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_centerInParent="true" 
         android:background="@drawable/bg_forum_tab" 
         app:tabIndicatorColor="@color/colorBtnBg" 
         app:tabIndicatorHeight="0dp" 
         app:tabPaddingBottom="-1dp" 
         app:tabPaddingEnd="-1dp" 
         app:tabPaddingStart="-1dp" 
         app:tabPaddingTop="-1dp" 
         style="@style/CustomTabLayoutStyle" 
         app:tabTextColor="@color/colorWhite" /> 
    

    設置tabPaddingStart/tabPaddingEnd/tabPaddingTop/tabPaddingBottom這樣。

    Styles: 
    
        <style name="CustomTabLayoutStyle" parent="Base.Widget.Design.TabLayout"> 
         <item name="tabSelectedTextColor">@color/tab_text_selected</item> 
         <item name="tabIndicatorColor">@color/tab_indicator</item> 
         <item name="tabTextAppearance">@style/CustomTabTexStyle</item> 
        </style> 
    
        <style name="CustomTabTexStyle" parent="TextAppearance.Design.Tab"> 
         <item name="android:textSize">14sp</item> 
         <item name="android:textColor">@color/tab_text</item> 
    
         ... 
        </style>