如何设置背景颜色TabHost [英] How to Set Background Color TabHost

查看:178
本文介绍了如何设置背景颜色TabHost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助,我发现难度在TabHost更改背景颜色。

原始图片:

我需要修改背景颜色像下面的图像。

我试了很多事情在我的code和XML过,但失败了。

我的$ C $低于C:

  TabHost tabHost = getTabHost();

        //标签1
        则tabspec aba1spec = tabHost.newTabSpec(标签1);
        //设置标题和图标标签
        tabHost.getTabWidget()setStripEnabled(假)。
        aba1spec.setIndicator(,getResources()getDrawable(R.drawable.tabenviaarq));
        意图photosIntent =新的意图(这一点,MainActivity.class);
        aba1spec.setContent(photosIntent);

    //将所有则tabspec到TabHost
        tabHost.addTab(aba1spec); //添加TAB1
 

在XML我有这样的:

 < XML版本=1.0编码=UTF-8&GT?;
< TabHost的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@机器人:ID / tabhost
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>

    < RelativeLayout的
        机器人:方向=垂直
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT>
        <的FrameLayout
            机器人:ID =@机器人:ID / tabcontent
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT
            机器人:layout_above =@机器人:ID /标签
            机器人:layout_alignParentTop =真/>
        < TabWidget
            机器人:ID =@机器人:ID /标签
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =65dp
            机器人:layout_alignParentBottom =真
            机器人:layout_marginBottom = -  5DP
            机器人:后台=#000000/>
    < / RelativeLayout的>
< / TabHost>
 

有人有一些想法,我非常感谢。

解决方案

  tabHost.setOnTabChangedListener(新OnTabChangeListener(){

        公共无效onTabChanged(字符串为arg0){
            的for(int i = 0; I< tab.getTabWidget()getChildCount();我++){
                tab.getTabWidget()。getChildAt㈠
                        .setBackgroundResource(R.drawable.tab_selected); //未选
            }
            tab.getTabWidget()。getChildAt(tab.getCurrentTab())
                    .setBackgroundResource(R.drawable.tab_unselected); //选择

        }
    });
 

试试这个方法,我希望这会帮助你。

I need help, I'm finding difficulty for change background color in a TabHost.

Original Image:

I need to modify background color like image below.

I tried many things in my code and XML too, but failed.

My code below:

 TabHost tabHost = getTabHost();

        // Tab 1
        TabSpec aba1spec = tabHost.newTabSpec("Tab 1");
        // setting Title and Icon for the Tab
        tabHost.getTabWidget().setStripEnabled(false);
        aba1spec.setIndicator("",getResources().getDrawable(R.drawable.tabenviaarq));
        Intent photosIntent = new Intent(this, MainActivity.class);
        aba1spec.setContent(photosIntent);

    // Adding all TabSpec to TabHost
        tabHost.addTab(aba1spec); // Adding tab1

in XML i have this:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_above="@android:id/tabs"
            android:layout_alignParentTop="true"/>
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="65dp"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="-5dp"
            android:background="#000000"/>
    </RelativeLayout>
</TabHost>

Somebody have some idea i thanks a lot.

解决方案

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

        }
    });

Try this method, I hope this will help you.

这篇关于如何设置背景颜色TabHost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆