2017-03-01 103 views
4

這是我的代碼:這是一個tabLayout我setupWith一個Viewpager如何以編程方式設置tabLayout的應用程序:tabBackground?

<android.support.design.widget.TabLayout 
     android:id="@+id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:fontFamily="sans-serif-light" 
     app:tabPaddingEnd="-1dp" 
     app:tabBackground="@drawable/tab_color_selector" 
     app:tabPaddingStart="-1dp" 
     app:tabTextAppearance="@style/MineCustomTabText" /> 

但我怎麼設置這個程序?

app:tabBackground="@drawable/tab_color_selector" 

,因爲我想要的顏色取決於用戶有選擇

這些是我已經嘗試過,但沒有他們的工作主題更改以編程方式設置此tabBackground這是非常重要的:

tabLayout.setBackground(getResources().getDrawable(R.drawable.tab_color_selector)); 
    tabLayout.setBackgroundResource((R.drawable.tab_color_selector)); 
    tabLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_color_selector)); 
    tabLayout.setBackground(ContextCompat.getDrawable(this, R.drawable.tab_color_selector)); 

注:

這是我在tab_color_selector.xml繪製:

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

回答

4

如果你想改變你可以使用這個選定的選項卡背景: (設置自定義視圖設置後viewPager)

TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);  
    tabLayout.setupWithViewPager(mViewPager); 
    tabLayout.getTabAt(tabLayout.getSelectedTabPosition()).setCustomView(R.layout.your_layout); 


如果你想改變tabLayout背景使用此:

TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 
tabLayout.setBackground(ContextCompat.getDrawable(this, R.drawable.your_drawable)); 

如果你使用的API級> 21使用它,而ContextCompat這樣的:

tabLayout.setBackground(getDrawable(R.drawable.badge)); 

實施例:

佈局

<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:fitsSystemWindows="true" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:theme="@style/Toolbar" 
     app:popupTheme="@style/Toolbar.Popup" /> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tab_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:tabMaxWidth="0dp" 
     app:tabGravity="fill" 
     app:tabMode="fixed" /> 

</android.support.design.widget.AppBarLayout> 

繪製對象

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval"> 
    <solid android:color="@color/red" /> 
</shape> 
+0

我想設置一個可繪製作爲背景,而不是顏色 – TSR

+0

@TSR我更新了響應,此代碼的工作 – MarcGV

+1

對不起,但仍不起作用。我試圖以編程方式設置背景是一個tabBackground,你的代碼是用於正常背景的 – TSR

0

嘗試THI秒。

ViewGroup tabStrip = (ViewGroup) tabLayout.getChildAt(0); 
for (int i = 0; i < tabStrip.getChildCount(); i++) { 
    View tabView = tabStrip.getChildAt(i); 
    if (tabView != null) { 
     int paddingStart = tabView.getPaddingStart(); 
     int paddingTop = tabView.getPaddingTop(); 
     int paddingEnd = tabView.getPaddingEnd(); 
     int paddingBottom = tabView.getPaddingBottom(); 
     ViewCompat.setBackground(tabView, AppCompatResources.getDrawable(tabView.getContext(), tabViewBgResId)); 
     ViewCompat.setPaddingRelative(tabView, paddingStart, paddingTop, paddingEnd, paddingBottom); 
    } 
} 
0

你可以這樣說:

Field field; 
try { 
    field = tabLayout.getClass().getDeclaredField("mTabBackgroundResId"); 
    field.setAccessible(true); 
    field.set(tabLayout, R.drawable.tab_background); 
} catch (NoSuchFieldException e) { 
    e.printStackTrace(); 
} catch (IllegalAccessException e) { 
    e.printStackTrace(); 
} 
相關問題