首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何用XML改变选项卡的背景?

如何用XML改变选项卡的背景?
EN

Stack Overflow用户
提问于 2011-05-10 05:47:55
回答 2查看 1.3K关注 0票数 0

我有一台带TabWidget的TabHost。我想要自定义选项卡的选中和未选中状态。我不确定如何将所有这些XML文件放在一起,这样我才能拥有自定义的选项卡?

代码语言:javascript
运行
复制
<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">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp">
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:padding="5dp" />
            </LinearLayout>
        </TabHost>

下面是我的选择器xml:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--  Active tab -->
    <item android:state_selected="true" android:state_focused="false"
        android:state_pressed="false" android:drawable="@drawable/tab_selected" />
    <!--  Inactive tab -->
    <item android:state_selected="false" android:state_focused="false"
        android:state_pressed="false" android:drawable="@drawable/tab_deselected" />
    <!--  Pressed tab -->
    <item android:state_pressed="true" android:drawable="@android:color/transparent" />
    <!--  Selected tab (using d-pad) -->
    <item android:state_focused="true" android:state_selected="true"
        android:state_pressed="false" android:drawable="@android:color/transparent" />
</selector>

下面是选中的状态:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:startColor="#97150c" android:centerColor="#7F7F7F"
        android:endColor="#b4190d" android:angle="-90" />
</shape>

以下是未选中的状态:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:startColor="#acacac" android:centerColor="#7F7F7F"
        android:endColor="#d7d7d7" android:angle="-90" />
</shape>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-05-10 07:00:56

下面的代码可以做到这一点,其中tw是TabWidget

代码语言:javascript
运行
复制
    for (int i = 0; i < tw.getChildCount(); i++)
    {
        View v = tw.getChildAt(i);
        v.setBackgroundDrawable(null);
        v.setOnTouchListener(new OnTouchListener()
        {

            @Override
            public boolean onTouch(View v, MotionEvent event)
            {

                // Change the icon and background colors
                TabWidget tw = getTabWidget();
                for (int i = 0; i < tw.getChildCount(); i++)
                {
                    View vv = tw.getChildAt(i);
                    vv.setBackgroundDrawable(null);
                    vv.setId(0);
                }

                NonScalingBackgroundDrawable nsbd = new NonScalingBackgroundDrawable(getApplicationContext(), v, R.drawable.nav_highlight);

                v.setBackgroundDrawable(nsbd);

                v.setId(1);
                return false;

            }

        });
    }
}

票数 0
EN

Stack Overflow用户

发布于 2011-05-10 05:50:40

在代码中做这件事是最容易的。只需遍历选项卡,并在每个选项卡上执行setBackgroundImage。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5942885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档