我在这里生成了一个自定义操作栏,除了选项卡之外,一切都可以正常工作。无论如何,选项卡指示器和选项卡背景颜色都保持不变。
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"/>
但这行不通。知道我该如何解决这个问题吗?
你可以简单地做到这一点
应用:标签指示器颜色
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"/>
您可以在 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;
}
我想您需要修改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>