1

嗨,我使用選項卡布局與查看尋呼機和FragmentPagerAdapter我使用自定義佈局來定義每個視圖根據position.Now我想要實現這個東西在這個圖像中看到(選擇一個是比其他人更白)。如何使用此圖像中的不同陰影突出顯示自定義視圖。我的所有選項卡都是相同的選擇或取消選擇。 早些時候我使用默認文本,它正在工作,但現在我使用自定義視圖與圖像和文本,我不知道如何將其應用到自定義視圖。 enter image description here突出顯示選項卡視圖尋呼機android

+0

HTTP://www.androidhive。 info/2015/09/android-material-design-working-with-tabs/ – 2016-03-03 11:22:49

+0

在這篇文章中,這個問題也沒有爲自定義視圖解決 –

+0

我認爲指標顏色會改變那個黃色線的顏色而不會爲自定義佈局添加不透明度 –

回答

0

這工作:

...onCreate(){ 
...  
tabHost.setOnTabChangedListener(new OnTabChangeListener() { 
@Override public void onTabChanged(String arg0) {  

setTabColor(tabHost); 
    } 
});  
setTabColor(tabHost); 
... 
} //Change The Backgournd Color of Tabs 

public void 
setTabColor(TabHost tabhost) { 
for(int i=0;i<tabhost.getTabWidget().getChildCount();i++)  
    tabhost.getTabWidget().getChildAt(i).setBackgroundColor(COLOR_CYAN); //unselected 
if(tabhost.getCurrentTab()==0) { 
tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()) 
.setBackgroundColor(COLOR_RED); //1st tab selected 
}else{ tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(COLOR_BLUE); //2nd tab selected 
} 

編輯

這裏的代碼片段覆蓋文本樣式和選定的文本顏色

<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout"> 
<item name="tabTextAppearance">@style/MyCustomTabText</item> 
<item name="tabSelectedTextColor">@color/tab_text_act</item> 
</style> 
<style name="MyCustomTabText" parent="TextAppearance.AppCompat.Button"> 
<item name="android:textSize">14sp</item> 
<item name="android:textColor">@color/tab_text</item> 
</style> 

,這裏是佈局

代碼片段
<android.support.design.widget.TabLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
style="@style/MyCustomTabLayout" /> 
+0

我正在使用選項卡布局而不使用TabHost它可以解決沒有TabHost? –

+0

@SahilShokeen我編輯了我的答案。 – Stanojkovic

+0

它會適用於圖標和文本,因爲我想將不透明度50%添加到未選中的選項卡,我想知道如何根據選項卡選擇將不透明度應用於自定義佈局? –

0

指定tabIndicatorColor = 「#00000000」 象下面這樣: -

<android.support.design.widget.TabLayout 
        android:id="@+id/tabs" 
        android:background="@android:color/white" 
        app:tabIndicatorColor="#00000000" 
        style="@style/MyCustomTabLayout" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 

對於不透明度和其它東西參閱下面的鏈接: -

TabLayout

+0

我想添加不透明度。 –

+0

檢查編輯的信息 –

相關問題