如何以编程方式设置tabLayout的应用程序:tabBackground?

问题描述:

这是我的代码:这是一个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> 

如果你想改变你可以使用这个选定的选项卡背景: (设置自定义视图设置后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

尝试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); 
    } 
} 

你可以这样说:

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