2013-10-13 3 views
2

나는 내 애플 리케이션 중 하나에 대한 안드로이드에서 탭 위젯을 사용하려고합니다. 이 탭 위젯에는 두 개의 탭이 있습니다. 나는 다음과 같은 두 가지 배경 이미지를 가지고있다. 사용자가 탭을 클릭하면 이미지가 변경됩니다. 내 상황에서 어떻게 가능할까요? 다음은 XML 파일에 내 코드는안드로이드에서 tabwidget의 이런 상황을 극복하는 방법

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

다음 코드는 자바에서입니다

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 

enter image description here

enter image description here

때 다음과 같이 나는 두 이미지를 사용자 c 이러한 이미지는 내가

답변

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>