Android,FragmentTabHost - 如何删除蓝色?

时间:2013-08-17 20:13:50

标签: android styles fragment-tab-host

我正在尝试删除android.support.v4.app.FragmentTabHost上标签和蓝色背景下方的蓝线(选中标签时)。有人做过吗?

的Tx

1 个答案:

答案 0 :(得分: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 / layout / 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;
    }
    }