尝试更改标签指示器颜色

时间:2013-10-29 21:16:15

标签: android xml android-tabhost

我已经生成了一个自定义操作栏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" />

考虑到文档,我需要使用此xml文件覆盖选项卡布局的背景属性。但是我该怎么办呢?我试过这样做:

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

但这不起作用。任何想法我怎么能解决这个问题?

3 个答案:

答案 0 :(得分:2)

您可以通过

完成此操作
  

应用程式:tabIndicatorColor

xml中的

属性

<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"/>

答案 1 :(得分: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 :(得分: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>