【问题标题】:How to change the background of a Tab with XML?如何使用 XML 更改选项卡的背景?
【发布时间】:2011-05-09 21:47:55
【问题描述】:

我有一个带有 TabWidget 的 TabHost。我想自定义选项卡的选定和未选定状态。我不确定如何将所有这些 XML 文件放在一起,以便我可以自定义选项卡?

<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:

<?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>

这里是选中状态:

<?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>

这里是未选中状态:

<?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>

【问题讨论】:

    标签: java android eclipse tabs android-tabhost


    【解决方案1】:

    下面的代码会做,其中 tw 是 TabWidget '

        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;
    
                }
    
            });
        }
    }
    

    '

    【讨论】:

      【解决方案2】:

      在代码中最容易做到这一点。只需遍历选项卡,并在每个选项卡上设置背景图像。

      【讨论】:

      • 但是什么时候选择它,什么时候取消选择等等呢?我将如何在代码中设置该状态?
      猜你喜欢
      • 1970-01-01
      • 2018-01-19
      • 2013-04-01
      • 1970-01-01
      • 2017-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多