2013-08-17 32 views

回答

1

我认为你必须定制你的标签。这里是我的项目代码,可能会给你一些建议。

在您的片段类

private TabHost.TabSpec createTab(String _tabText, boolean _canClose) { 
    TabFactory tf = new TabFactory(this); 
    tf.setOnTabClosedListener(this); 
    TabHost.TabSpec spec = mTabHost.newTabSpec(_tabText); 
    spec.setIndicator(tf.createTabView(_tabText, _canClose)); 
    spec.setContent(tf); 
    return spec; 
} 

在你RES /布局/ your_xml

//tab_pagers.xml 
    <?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="@dimen/tab_width" 
    android:layout_height="fill_parent" 
    android:background="@drawable/selector_tab_pagers" 
    android:fadingEdge="none" > 

    <TextView 
     android:id="@+id/tv_tab_text" 
     style="@style/Text.Tab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" /> 

    <ImageView 
     android:id="@+id/btn_close_tab" 
     android:layout_width="20dp" 
     android:layout_height="20dp" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:background="@android:color/transparent" 
     android:paddingRight="5dp" 
     android:paddingTop="5dp" 
     android:src="@drawable/ic_close" /> 

    </RelativeLayout> 

在你TabClass

//TabFactory 
    public final class TabFactory implements TabHost.TabContentFactory { 

    private static final int LAYOUT_TAB = R.layout.tab_pagers; 
    private final Context mContext; 


    public interface OnTabClosedListener { 

     void onTabClosed(String _tabText); 
    } 


    OnTabClosedListener mOnTabClosedListener; 


    public TabFactory(Context _context) { 
     super(); 
     mContext = _context; 
    } 


    @Override 
    public View createTabContent(String tag) { 
     View v = new View(mContext); 
     v.setMinimumWidth(0); 
     v.setMinimumHeight(0); 
     return v; 
    } 


    public View createTabView(final String _tabText, boolean _canClose) { 
     View view = View.inflate(mContext, LAYOUT_TAB, null); 
     TextView tv = (TextView) view.findViewById(R.id.tv_tab_text); 
     view.findViewById(R.id.btn_close_tab).setVisibility(_canClose ? View.VISIBLE : View.INVISIBLE); 
     view.findViewById(R.id.btn_close_tab).setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View _v) { 
       if (mOnTabClosedListener != null) { 
        mOnTabClosedListener.onTabClosed(_tabText); 
       } 
      } 
     }); 
     tv.setText(_tabText); 
     return view; 
    } 


    public void setOnTabClosedListener(OnTabClosedListener _onTabClosedListener) { 
     mOnTabClosedListener = _onTabClosedListener; 
    } 
    }