2010-10-12 91 views
0

我希望能夠分配不同的圖像到TabLayout控件中的我的選項卡,具體取決於是否選擇該項目。我遵循Android網站上的教程,但他們只用一個圖像做了例子,並且它的工作原理。但它不適用於其他選項卡。我怎樣才能使它工作?這是我的代碼:Android標籤佈局教程?

主要活動:

public class Main extends TabActivity { 
    private Resources res; 
    private TabHost tabHost; 
    private TabHost.TabSpec spec; // Resusable TabSpec for each tab 
    private Intent intent; // Reusable Intent for each tab 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // The IDs in main2 should be exactly like the current ones. 
     setContentView(R.layout.main2); 

     res = getResources(); // Resource object to get Drawables 
     tabHost = getTabHost(); // The activity TabHost 
     // Create an Intent to launch an Activity for the tab (to be reused) 
     intent = new Intent().setClass(this, Tab1.class); 

     // Initialize a TabSpec for each tab and add it to the TabHost 
     spec = tabHost.newTabSpec("artists").setIndicator("Artists", 
          res.getDrawable(R.drawable.ic_tab_artists_grey)) 
         .setContent(intent); 
     tabHost.addTab(spec); 

     // Do the same for the other tabs 
     intent = new Intent().setClass(this, Tab2.class); 
     spec = tabHost.newTabSpec("albums").setIndicator("Albums", 
          res.getDrawable(R.drawable.ic_tab_albums_grey)) 
         .setContent(intent); 
     tabHost.addTab(spec); 

     intent = new Intent().setClass(this, Tab3.class); 
     spec = tabHost.newTabSpec("songs").setIndicator("Songs", 
          res.getDrawable(R.drawable.ic_tab_songs_grey)) 
         .setContent(intent); 
     tabHost.addTab(spec); 

     tabHost.setCurrentTab(1); 
    } 
} 

主要XML:

<?xml version="1.0" encoding="UTF-8"?> 
<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 version="1.0" encoding="UTF-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- When selected, use grey --> 
    <item android:drawable="@drawable/ic_tab_artists_grey" 
      android:state_selected="true" /> 
    <!-- When not selected, use white--> 
    <item android:drawable="@drawable/ic_tab_artists_white"/> 
</selector> 

的選擇第二標籤:

<?xml version="1.0" encoding="UTF-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- When selected, use grey --> 
    <item android:drawable="@drawable/ic_tab_songs_grey" 
      android:state_selected="true" /> 
    <!-- When not selected, use white--> 
    <item android:drawable="@drawable/ic_tab_songs_white"/> 
</selector> 

等我的圖像被稱爲ic_tab_songs_white,ic_tab_songs_grey,ic_tab_albums_white,ic_tab_albums_grey,ic_tab_artists_white,ic_tab_artists_grey。

回答

4

難道問題是您將選項卡背景定義爲您擁有的圖像而不是您定義的選擇器?從你的文本中不清楚你如何命名你有代碼示例的兩個選擇器文件,但你的代碼應該引用這些文件,而不是實際的圖像。

+0

是的,那是正確的答案。謝謝 :) – 2010-10-15 11:40:15