【问题标题】:How to change the background color of the TabWidget (xml only)如何更改 TabWidget 的背景颜色(仅限 xml)
【发布时间】:2014-12-08 19:12:49
【问题描述】:

这个问题已经被问过很多次了,但我找不到只使用适合我的 xml 样式的明确解决方案。因此,在其他示例中,再次使用了操作栏中的选项卡,这不是我的情况。

在清单中:

android:theme="@style/AppTheme"

在 style.xml 中:

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:actionBarTabStyle">@style/MyActionBarTabs</item>
        <item name="actionBarTabStyle">@style/MyActionBarTabs</item>
    </style>

<style name="MyActionBarTabs" parent="@style/Widget.AppCompat.ActionBar.TabView">
        <item name="android:background">@drawable/actionbar_tab_indicator</item>
        <item name="background">@drawable/actionbar_tab_indicator</item>
    </style>

在 actionbar_tab_indicator.xml 中:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- STATES WHEN BUTTON IS NOT PRESSED -->

    <!-- Non focused states -->
    <item android:state_focused="false" android:state_selected="false"
          android:state_pressed="false"
          android:drawable="@color/red_spec" />
    <item android:state_focused="false" android:state_selected="true"
          android:state_pressed="false"
          android:drawable="@color/red_spec" />

    <!-- Focused states (such as when focused with a d-pad or mouse hover) -->
    <item android:state_focused="true" android:state_selected="false"
          android:state_pressed="false"
          android:drawable="@color/red_spec" />
    <item android:state_focused="true" android:state_selected="true"
          android:state_pressed="false"
          android:drawable="@color/red_spec" />


<!-- STATES WHEN BUTTON IS PRESSED -->

    <!-- Non focused states -->
    <item android:state_focused="false" android:state_selected="false"
          android:state_pressed="true"
          android:drawable="@color/red_spec" />
    <item android:state_focused="false" android:state_selected="true"
        android:state_pressed="true"
        android:drawable="@color/red_spec" />

    <!-- Focused states (such as when focused with a d-pad or mouse hover) -->
    <item android:state_focused="true" android:state_selected="false"
          android:state_pressed="true"
          android:drawable="@color/red_spec" />
    <item android:state_focused="true" android:state_selected="true"
          android:state_pressed="true"
          android:drawable="@color/red_spec" />
</selector>

使用上面的这种样式组合根本不起作用。它不会改变任何东西。

如果我在下面使用这种样式,它仅适用于更改字体样式,但不适用于背景:

 <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:tabWidgetStyle">@style/CustomTabWidget</item>
            </style>
 <style name="CustomTabWidget" parent="@android:style/Widget.TabWidget">
        <item name="android:textAppearance">@style/CustomTabWidgetText</item>
        <item name="android:background">@color/red_overlay_spec</item>
    </style>

    <style name="CustomTabWidgetText" parent="@android:style/TextAppearance.Widget.TabWidget">
        <item name="android:textSize">25sp</item>
        <item name="android:textStyle">bold</item>
    </style>

这是我用来制作基于 TabHost 的片段布局的片段:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center_horizontal"
    android:orientation="vertical"
    android:background="@color/core_blue_bgr" >

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"

         >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:orientation="horizontal" />

            <!-- tab host should have frameLayout with this ID 
            but is not use for content-->
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="0" />
            <!-- the real content is put here -->
            <FrameLayout
                android:id="@+android:id/realtabcontent"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />
        </LinearLayout>
    </TabHost>
</LinearLayout>

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    在第一个选项卡背景颜色更改中只有一次,但之后当我选择其他选项卡时颜色不会改变

    你可以只设置一次(onCreate only) 尝试像这样在你的 setOnTabChangedListener 上设置它

    tabHost.setOnTabChangedListener(new OnTabChangeListener() {
    
    @Override
    public void onTabChanged(String tabId) {
    setTabColor(tabHost);
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多