【问题标题】:Trying to change tab indicator color尝试更改选项卡指示器颜色
【发布时间】:2013-10-29 21:16:14
【问题描述】:

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

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

【问题讨论】:

    标签: android xml android-tabhost


    【解决方案1】:

    你可以简单地做到这一点

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 我真的很想知道一种在 XML 中执行此操作的方法。
      • 请检查我的答案
      【解决方案3】:

      我猜你需要修改 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>
      

      【讨论】:

      • 我拥有的唯一与标签有关的 xml 文件是我在原始帖子中提供的那个
      • @Matthew 尝试使用我为测试提供的内容创建 tab_selected_recorder.xml 怎么样?
      猜你喜欢
      • 2015-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多