1

我在这里生成了一个自定义操作栏,除了选项卡外一切正常。无论如何,选项卡指示器和选项卡背景颜色保持不变。

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

但这不起作用。知道我该如何解决这个问题吗?

4

3 回答 3

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"/>
于 2015-08-11T18:47:25.533 回答
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;
}
于 2013-10-29T21:41:28.517 回答
0

我猜你需要修改你的tabxml 文件,比如tab_selected_recorder.xml根据你的需要改变标签的背景。如果您提供这些文件之一,可能会有所帮助。例如tab_selected_recorder.xml可能是这样的:

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" >
    <solid android:color="#888888" />
</shape>
于 2013-10-29T21:32:12.773 回答