2013-10-29 54 views
1

我已生成自定義操作欄here,除標籤外,其他所有工作都可以使用。無論如何,選項卡指示符和選項卡背景顏色保持不變。試圖更改選項卡指示器顏色

tab_indicator_ab_recorder.xml文件:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<!-- Non focused states --> 
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected_recorder" /> 
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected_recorder" /> 

<!-- Focused states --> 
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected_focused_recorder" /> 
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected_focused_recorder" /> 

<!-- Pressed --> 
<!-- Non focused states --> 
<item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_recorder" /> 
<item android:state_focused="false" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_recorder" /> 

<!-- Focused states --> 
<item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_recorder" /> 
<item android:state_focused="true" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_recorder" /> 

Accourding的文檔,我需要這個xml文件來覆蓋標籤佈局的背景屬性。但我該怎麼做?我試過這樣做:

<TabWidget 
    android:id="@android:id/tabs" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/tab_indicator_ab_recorder"/> 

但這是行不通的。任何想法我怎麼能解決這個問題?

回答

1

你可以指定你的java代碼的標籤指示。以下是我在設置標籤主機時所做的。添加完整的代碼和兩個標籤以進行說明。儘管getTabIndicator()方法是相關的部分。

private void initTabs(String currentTab) { 
    mTabHost = (TabHost) mRootView.findViewById(R.id.orderTabHost); 
    mTabHost.setup(); 

    // Add drawing tab 
    TabHost.TabSpec drawingTab = mTabHost.newTabSpec(DRAWING_TAB_TAG); 
    drawingTab.setContent(R.id.tab_drawing_container); 
    String drawingTitle = getResources().getString(R.string.drawing_title); 
    drawingTab.setIndicator(getTabIndicator(drawingTitle)); 
    mTabHost.addTab(drawingTab); 

    // Add detail tab 
    TabHost.TabSpec detailTab = mTabHost.newTabSpec(DETAIL_TAB_TAG); 
    detailTab.setContent(R.id.tab_info_container); 
    String detailTitle = getResources().getString(R.string.detail_title); 
    detailTab.setIndicator(getTabIndicator(detailTitle)); 
    mTabHost.addTab(detailTab); 
} 

// Call this for every tab. 
private View getTabIndicator(String tabTitle) { 
    View tabIndicator = LayoutInflater.from(getActivity()).inflate(
      R.layout.tab_indicator_holo, mTabHost.getTabWidget(), false); 
    TextView title = (TextView) tabIndicator 
      .findViewById(android.R.id.title); 
    title.setText(tabTitle); 
    return tabIndicator; 
} 
+2

我真的想知道如何在XML中做到這一點。 –

+1

請檢查我的答案 – Max

0

我想你需要修改你的tab xml文件,如tab_selected_recorder.xml根據你的需要改變標籤的背景。如果您提供了其中一個文件,這可能會有所幫助。例如tab_selected_recorder.xml可能是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<shape android:shape="rectangle" > 
    <solid android:color="#888888" /> 
</shape> 
+1

有事情做與標籤的唯一的XML文件,我有我在原始文章中提供的那個 – Guy

+1

@Mthethew如何嘗試創作te tab_selected_recorder.xml中提供了用於測試的內容? – kiruwka

2

只需通過

應用做到這一點:在XML tabIndicatorColor

財產

<android.support.design.widget.TabLayout 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@id/pages_tabs" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="?attr/colorPrimary" 
android:minHeight="?attr/actionBarSize" 
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
app:tabIndicatorColor="@android:color/white" 
app:tabIndicatorHeight="4dp"/> 
相關問題