這是我的代碼:這是一個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>
我想設置一個可繪製作爲背景,而不是顏色 – TSR
@TSR我更新了響應,此代碼的工作 – MarcGV
對不起,但仍不起作用。我試圖以編程方式設置背景是一個tabBackground,你的代碼是用於正常背景的 – TSR