2011-10-04 69 views
0

在我的應用程序中,我使用自定義選項卡。在這裏,我正在爲圖像視圖編程添加圖標。除第一個選項卡外,圖像正確添加。請任何人都可以幫助我。 以下是我的代碼。我沒有得到自定義選項卡的圖標

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tabsLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/tab_bg_selector" 
    android:padding="2dip" 
    android:gravity="center" 
    android:orientation="vertical"> 

    <ImageView android:id="@+id/tabicon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"   
    /> 
    <TextView android:id="@+id/tabsText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Title" 
     android:textSize="12dip" 
     android:textColor="@drawable/tab_text_selector" 
    /> 

</LinearLayout> 

Java代碼

public class CustomTabActivity extends Activity { 

    private TabHost mTabHost; 
    private void setupTabHost() { 
     mTabHost = (TabHost) findViewById(android.R.id.tabhost); 
     mTabHost.setup(); 
    } 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // construct the tabhost 
     setContentView(R.layout.main); 

     setupTabHost(); 
     mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider); 

     setupTab(new TextView(this), "Find Gifts",R.drawable.findgifts); 
     setupTab(new TextView(this), "Tracked Items",R.drawable.items); 
     setupTab(new TextView(this), "Preferences",R.drawable.preferences); 
    } 

    private void setupTab(final View view, final String tag,int drawable) { 
     View tabview = createTabView(mTabHost.getContext(), tag, drawable); 

     TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() { 
      public View createTabContent(String tag) { 
       return view; 
      } 
     }); 
     mTabHost.addTab(setContent); 
    } 

    private static View createTabView(final Context context, final String text,int drawable) { 
     View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null); 
     ImageView tabicon = (ImageView) view.findViewById(R.id.tabicon);  
     tabicon.setBackgroundResource(drawable); 
     TextView tv = (TextView) view.findViewById(R.id.tabsText); 
     tv.setText(text);  
     return view; 
    } 
} 

回答