2011-07-30 94 views

回答

0

簡單的方法來做到這一點:

  1. 創建自定義佈局:

    private void addTab(String text, int drawable) { 
        TabHost.TabSpec spec = mTabHost.newTabSpec(text); 
    
        View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false); 
    
        TextView title = (TextView) tabIndicator.findViewById(R.id.title); 
        title.setText(text); 
        ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon); 
        icon.setImageResource(drawable); 
    
        spec.setIndicator(tabIndicator);  
    
        mTabHost.addTab(spec.setContent(this)); 
    
    } 
    

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="0dip" 
    android:layout_height="64dip"  
    android:layout_weight="1" 
    android:orientation="vertical" 
    android:background="@drawable/tab_indicator" 
    > 

    <ImageView android:id="@+id/icon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:paddingTop="7dip" 
    /> 

    <TextView android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     style="?android:attr/tabWidgetStyle" 
     android:paddingBottom="5dip" 
    />  
</RelativeLayout> 
  • ,使用此佈局添加標籤