我正在尝试在我的应用程序中使用android中的Tab Widget。 此选项卡小部件中有两个选项卡。我有两个背景图像如下。当用户点击任何标签时,应该更改图像。在我的情况下怎么可能呢? 以下是我在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
我有两张图片如下
当用户点击任何标签时,这些图像应该互换,我该怎么做
答案 0 :(得分: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>