如何更改 Android 选项卡小部件的背景?

发布于 2024-08-18 07:26:06 字数 365 浏览 6 评论 0原文

我的类扩展了 TabActivity

TabHost mTabHost =  getTabHost();

TabHost.TabSpec tab1 =mTabHost.newTabSpec("tab1");
TabHost.TabSpec tab2 =mTabHost.newTabSpec("tab2");

tab1 .setIndicator("title tab1");
tab2 .setIndicator("title tab2");
mTabHost.addTab(tab1);mTabHost.addTab(tab2);

TabHost.setCurrentTab(0 or 1)

谁能指导我如何更改所选选项卡的背景图像或颜色?

My class extends extends TabActivity

TabHost mTabHost =  getTabHost();

TabHost.TabSpec tab1 =mTabHost.newTabSpec("tab1");
TabHost.TabSpec tab2 =mTabHost.newTabSpec("tab2");

tab1 .setIndicator("title tab1");
tab2 .setIndicator("title tab2");
mTabHost.addTab(tab1);mTabHost.addTab(tab2);

TabHost.setCurrentTab(0 or 1)

Can anybody guide me how do I change the background image or color of selected tab?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

舞袖。长 2024-08-25 07:26:06

这将设置您的选项卡颜色:

public static void setTabColor(TabHost tabhost) {
    for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) {
        tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FF0000")); //unselected
    }
    tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("#0000FF")); // selected
}

如果您将其放在 onTabChangedListener() 中,它将为所选选项卡保留正确的颜色。

This will set your tab colors:

public static void setTabColor(TabHost tabhost) {
    for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) {
        tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FF0000")); //unselected
    }
    tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("#0000FF")); // selected
}

and if you put it within the onTabChangedListener(), it will keep the correct color for selected tabs.

一袭白衣梦中忆 2024-08-25 07:26:06

正如 mbaird 提到的,更好的解决方案是将 background 与选择器一起使用,这样您就不必检查 onTabChanged 并进行手动更新。最少的代码如下:

private void initTabsAppearance(TabWidget tabWidget) {
    // Change background
    for(int i=0; i < tabWidget.getChildCount(); i++)
        tabWidget.getChildAt(i).setBackgroundResource(R.drawable.tab_bg);
}

其中 tab_bg 是带有选择器的 xml 可绘制对象:

<selector xmlns:android="http://schemas.android.com/apk/res/android">    
    <item android:state_selected="true" android:drawable="@drawable/tab_bg_selected" />
    <item android:drawable="@drawable/tab_bg_normal" />
</selector>

对于完整的选项卡自定义,我将添加使用自定义主题更改选项卡文本样式的代码。将其添加到 styles.xml

<resources>

    <style name="MyCustomTheme" parent="@android:style/Theme.Light.NoTitleBar">
        <item name="android:tabWidgetStyle">@style/CustomTabWidget</item>
    </style>

    <style name="CustomTabWidget" parent="@android:style/Widget.TabWidget">
        <item name="android:textAppearance">@style/CustomTabWidgetText</item>
    </style>

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

</resources>

要使用此主题,请在 AndroidManifest.xml 中定义它:

<application android:theme="@style/MyCustomTheme">

现在您拥有具有自定义背景自定义文本样式<的选项卡小部件< /强>。

As mbaird mentioned, the better solution is to use background with selector, so you don't have to check onTabChanged and do manual update. The minimal code is here:

private void initTabsAppearance(TabWidget tabWidget) {
    // Change background
    for(int i=0; i < tabWidget.getChildCount(); i++)
        tabWidget.getChildAt(i).setBackgroundResource(R.drawable.tab_bg);
}

Where tab_bg is an xml drawable with selector:

<selector xmlns:android="http://schemas.android.com/apk/res/android">    
    <item android:state_selected="true" android:drawable="@drawable/tab_bg_selected" />
    <item android:drawable="@drawable/tab_bg_normal" />
</selector>

For the full Tab customization I will add the code for changing tab text style using custom theme. Add this to styles.xml:

<resources>

    <style name="MyCustomTheme" parent="@android:style/Theme.Light.NoTitleBar">
        <item name="android:tabWidgetStyle">@style/CustomTabWidget</item>
    </style>

    <style name="CustomTabWidget" parent="@android:style/Widget.TabWidget">
        <item name="android:textAppearance">@style/CustomTabWidgetText</item>
    </style>

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

</resources>

To use this theme, define it in AndroidManifest.xml:

<application android:theme="@style/MyCustomTheme">

And now you have tab widgets with custom background and custom text style.

爱给你人给你 2024-08-25 07:26:06

如果注册 TabHost.OnTabChanged 事件并调用 mTab​​Host.getCurrentTabView() 获取 View,然后调用 view.setBackgroundResource() 会怎么样?

What if you register for TabHost.OnTabChanged events and call mTabHost.getCurrentTabView() to get the View, then view.setBackgroundResource()?

猥︴琐丶欲为 2024-08-25 07:26:06

可以解决您的问题吗?基本上使用选择器在每个选项卡视图上调用 setBackgroundDrawable ?

Does this solve your problem? Basically calling setBackgroundDrawable on each tab view with a selector?

没︽人懂的悲伤 2024-08-25 07:26:06
>     TabHost mTabHost =  getTabHost();
>     
>     TabHost.TabSpec tab1 =mTabHost.newTabSpec("tab1");
>     TabHost.TabSpec tab2 =mTabHost.newTabSpec("tab2");
>     
>     tab1.setIndicator("title tab1");
>     tab2.setIndicator("title tab2");
>     mTabHost.addTab(tab1) ;mTabHost.addTab(tab2);
>     
>     TabHost.setCurrentTab(0 or 1);


mTabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab1selector); 

mTabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tab2selector);    

mTabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.tab3selector);    

mTabHost.getTabWidget().getChildAt(3).setBackgroundResource(R.drawable.tab4selector);

使用 .setBackgroundResource 且 tabNselector 是 XML - tabNselector.xml

    <?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_selected="false" android:drawable="@drawable/tabN"/>
   <item android:state_selected="true" android:drawable="@drawable/tabNsel"  />
</selector>
>     TabHost mTabHost =  getTabHost();
>     
>     TabHost.TabSpec tab1 =mTabHost.newTabSpec("tab1");
>     TabHost.TabSpec tab2 =mTabHost.newTabSpec("tab2");
>     
>     tab1.setIndicator("title tab1");
>     tab2.setIndicator("title tab2");
>     mTabHost.addTab(tab1) ;mTabHost.addTab(tab2);
>     
>     TabHost.setCurrentTab(0 or 1);


mTabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab1selector); 

mTabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tab2selector);    

mTabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.tab3selector);    

mTabHost.getTabWidget().getChildAt(3).setBackgroundResource(R.drawable.tab4selector);

Use .setBackgroundResource and tabNselector is an XML - tabNselector.xml

    <?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_selected="false" android:drawable="@drawable/tabN"/>
   <item android:state_selected="true" android:drawable="@drawable/tabNsel"  />
</selector>
关于从前 2024-08-25 07:26:06

我在 XML 的 TabWidget 元素中设置了“android:background”参数,以提供所有选项卡的通用背景。

然后我在“.setIndicator”方法中传递了从另一个 XML 扩充的视图。

 View v = LayoutInflater.from(this).inflate(R.layout.tab_widget, null);
    TextView label = (TextView) v.findViewById(R.id.tabLabel);
    label.setText("Whatever");
 tab1 .setContent(v);

我觉得这是一个更好的方法。

I set the 'android:background' parameter in the TabWidget element of the XML to give the generic background of all the tabs.

Then I passed views inflated from another XML in the '.setIndicator' method.

 View v = LayoutInflater.from(this).inflate(R.layout.tab_widget, null);
    TextView label = (TextView) v.findViewById(R.id.tabLabel);
    label.setText("Whatever");
 tab1 .setContent(v);

I feel that's a nicer way of doing this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文