簡體   English   中英

如何使用XML更改選項卡的背景?

[英]How to change the background of a Tab with XML?

我有一個帶有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>

用代碼做起來最簡單。 只需遍歷選項卡,然后在每個選項卡上設置setBackgroundImage。

以下代碼將完成此操作,其中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;

            }

        });
    }
}

'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM