2

我正在尝试将 android 中的标签小部件用于我的一个应用程序。此选项卡小部件中有两个选项卡。我有两个背景图像,如下所示。当用户单击任何选项卡时,图像应该会发生变化。在我的情况下怎么可能?以下是我在 xml 文件中的代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relative_my_student_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF" 
    android:padding="-4dp"
    >

    <RelativeLayout
        android:id="@+id/relative_top"
        android:layout_width="fill_parent"
        android:layout_height="140dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/header" >
    </RelativeLayout>

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/footer"
        android:layout_below="@+id/relative_top" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <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="match_parent"
                android:layout_height="match_parent" >
            </FrameLayout>
        </LinearLayout>
    </TabHost>
</RelativeLayout>

以下代码来自java

    TabHost tabHost = getTabHost();

    // Tab for Photos
    TabSpec student_list = tabHost.newTabSpec("Photos");
    student_list.setIndicator("",        getResources().getDrawable(R.drawable.my_student_list_green_line));
    Intent photosIntent_intent = new Intent(this, Student_List_Activity.class);
    student_list.setContent(photosIntent_intent);

    // Tab for Songs
    TabSpec add_new_student = tabHost.newTabSpec("Songs");
    // setting Title and Icon for the Tab
    add_new_student.setIndicator("", getResources().getDrawable(R.drawable.my_student_add_new_student));
    Intent add_new_student_intent = new Intent(this, Add_New_Student.class);
    add_new_student.setContent(add_new_student_intent);

    // Tab for Videos

    // Adding all TabSpec to TabHost
    tabHost.addTab(student_list); // Adding photos tab
    tabHost.addTab(add_new_student); // Adding songs tab

我有两个图像如下

在此处输入图像描述

在此处输入图像描述

当用户当时单击任何选项卡时,这些图像应该可以互换我该怎么做

4

1 回答 1

5

试试这个解决方案

 //Tab Host 1
    getTabHost().addTab(getTabHost().newTabSpec("")
    //set the tab name
    .setIndicator("")
    //Add additional flags to the intent (or with existing flags value).
    .setContent(new Intent(this, StudentListActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))); 


    //Tab Host 2
    getTabHost().addTab(getTabHost().newTabSpec("")
    //set the tab name
    .setIndicator("")
    //Add additional flags to the intent (or with existing flags value).
    .setContent(new Intent(this,AddNewStudent.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) );
    getTabWidget().setStripEnabled(false);// this line is specifically used to remove the line in bottm at tabWidget 

    getTabHost().getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tabhost_student_images);
    getTabHost().getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tabhost_add_new_student_images);



    //On Drowable
    //tabhost_add_new_student_images.xml
    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/tab_second" android:state_selected="true"/>
    <item android:drawable="@drawable/my_student_add_new_student" android:state_selected="false"/>

    </selector>

    //tabhost_student_images.xml
    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/my_student_list_green_line" android:state_selected="true"/>
    <item android:drawable="@drawable/tab_add_new_student" android:state_selected="false"/>

    </selector>
于 2013-10-30T12:54:01.223 回答