2017-03-01 14 views
4

これは私のコードです:それは私setupWithViewpagerアプリを設定する方法: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" /> 

しかし、どのように私はプログラム的にこれを設定しますtabLayoutですか?

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

あなたが選択したタブの背景を変更したい場合は、この使用することができます:あなたはtabLayoutの背景を変更したい場合は (後viewPagerを設定するカスタムビューを設定します)

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


をこれを使用する:

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

あなたはこのようContextCompatずにAPIレベル> 21使用、それを使用している場合:

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> 

Drawableの

<?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

するTry THI s。あなたはこのようにそれを行うことができます

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(); 
} 
関連する問題