【问题标题】:Android - tabhost not change background colorAndroid - tabhost 不改变背景颜色
【发布时间】:2015-04-23 08:10:08
【问题描述】:

我已经设置了我的 tabhost 背景颜色,但它不起作用。

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarTabStyle">@style/ActionBarTabStyle</item>
    <item name="actionBarTabStyle">@style/ActionBarTabStyle</item>
</style>

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

tab_bg_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/blueLight" android:state_pressed="true"/>
    <item android:drawable="@color/blueLight" android:state_selected="true"/>
    <item android:drawable="@color/blueDark"/>
</selector>

bottom_tabs.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" />

    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />
    </android.support.v4.app.FragmentTabHost>

</LinearLayout>

【问题讨论】:

  • 你在使用 AppCompat 库吗?
  • 是的,编译'com.android.support:appcompat-v7:22.1.0'
  • 你能贴出tabvew的代码吗
  • 完成,您现在可以查看。

标签: android android-actionbar-compat android-actionbar-tabs


【解决方案1】:

由于您使用的是 AppCompat 库,因此请使用:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarTabStyle">@style/ActionBarTabStyle</item>
    <item name="actionBarTabStyle">@style/ActionBarTabStyle</item>
</style>

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

您需要 Widget.AppCompat.ActionBar.TabView 父级和 android: 变体来支持 AppCompat。

那是根据ActionBar's documentationActionBar Tab's documentation

【讨论】:

  • 是的,你也有ActionBarTabStyle 错了。它应该有Widget.AppCompat.ActionBar.TabView 作为父级,而不是android:Widget.ActionBar.TabView。再次检查:)
【解决方案2】:

如果您可以更改代码中的颜色,则可以尝试以下操作:

tabHost.setOnTabChangedListener(new OnTabChangeListener() {
    public void onTabChanged(String arg0) {
        for (int i = 0; i < tab.getTabWidget().getChildCount(); i++) {
            tab.getTabWidget().getChildAt(i)
                .setBackgroundResource(R.drawable.tab_selected); // unselected
        }

        tab.getTabWidget().getChildAt(tab.getCurrentTab())
            .setBackgroundResource(R.drawable.tab_unselected); // selected
    }
});

【讨论】:

  • 我无法实现 OnTabChangeListener,因为我使用的是 ActionBarActivity,而不是 TabActivity
【解决方案3】:

你可以试试:

tab_selector.xml

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

    <!-- Non focused states -->
    <item android:drawable="@drawable/unselected_tab" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>
    <item android:drawable="@drawable/button_buynow" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>
    <!-- Focused states -->
    <item android:drawable="@drawable/unselected_tab" android:state_focused="true" android:state_pressed="false" android:state_selected="false"/>
    <item android:drawable="@drawable/selected_tab" android:state_focused="true" android:state_pressed="false" android:state_selected="true"/>
    <!-- Pressed -->
    <!-- Non focused states -->
    <item android:drawable="@drawable/unselected_tab" android:state_focused="false" android:state_pressed="true" android:state_selected="false"/>
    <item android:drawable="@drawable/button_buynow" android:state_focused="false" android:state_pressed="true" android:state_selected="true"/>
    <!-- Focused states -->
    <item android:drawable="@drawable/unselected_tab" android:state_focused="true" android:state_pressed="true" android:state_selected="false"/>
    <item android:drawable="@drawable/button_buynow" android:state_focused="true" android:state_pressed="true" android:state_selected="true"/>

</selector>

在 MainActivity 中声明后,您可以使用以下代码:

TabWidget widget = th.getTabWidget();

    for (int i = 0; i < widget.getChildCount(); i++) {
        View v = widget.getChildAt(i);
        v.setBackgroundResource(R.drawable.tab_selector);
    }

【讨论】:

  • 我想要的是改变背景颜色,而不是背景图像。
  • TextView tv = (TextView) th.getTabWidget().getChildAt(1) .findViewById(android.R.id.title); // 未选中的标签 tv.setBackgroundColor(getResources().getColor(R.color.TransparentColor));
  • 对不起,我不是很明白。
  • 您可以通过将 tabhost 文本加载到 TextView 来更改背景颜色
【解决方案4】:

在您的主题中,添加以下内容

<item name="android:actionBarStyle">@style/MyActionBar</item>
<item name="actionBarStyle">@style/MyActionBar</item>

然后添加 MyActionBar 样式(用您自己的替换可绘制条目):

<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <!-- action bar background -->
    <item name="background">@drawable/ab_solid_mytabhost</item>
    <!-- needed for 'stacked' & 'split' action bar (used by tabhost) -->
    <item name="backgroundStacked">@drawable/ab_stacked_solid_mytabhost</item>
    <item name="backgroundSplit">@drawable/ab_bottom_solid_mytabhost</item>
</style>

您的 styles.xml 应该如下所示:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarTabStyle">@style/ActionBarTabStyle</item>
    <item name="actionBarTabStyle">@style/ActionBarTabStyle</item>
</style>

<style name="ActionBarTabStyle" parent="@style/Widget.AppCompat.ActionBar.TabView">
    <item name="android:background">@drawable/tab_bg_selector</item>
    <item name="background">@drawable/tab_bg_selector</item>
    <!-- add these -->
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>

<!-- add this-->
<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <!-- action bar background -->
    <item name="background">@drawable/ab_solid_mytabhost</item>
    <!-- needed for 'stacked' & 'split' action bar (used by tabhost) -->
    <item name="backgroundStacked">@drawable/ab_stacked_solid_mytabhost</item>
    <item name="backgroundSplit">@drawable/ab_bottom_solid_mytabhost</item>
</style>

对于一个工作示例,这里是一个示例项目的链接:https://github.com/ebernie/MyTabHostThemeSample

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2015-06-16
    • 1970-01-01
    • 2012-04-02
    相关资源
    最近更新 更多