簡體   English   中英

嘗試更改標簽指示器顏色

[英]Trying to change tab indicator color

我在這里生成了一個自定義操作欄,除標簽外一切正常。 無論如何,標簽指示符和標簽背景顏色保持不變。

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

但這不起作用。 知道我怎么能解決這個問題?

你可以簡單地做到這一點

應用: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"/>

您可以在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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM