Search code examples
androidstylesfragment-tab-host

Android, FragmentTabHost - how to remove blue?


I am trying to remove the blue line below tabs and blue background(when tab selected) on android.support.v4.app.FragmentTabHost. Anyone done that?

Tx


Solution

  • I think you have to customize your tabs. Here is codes from my project, might give you some suggestions.

    In your fragment class

    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;
    }
    

    In your 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>
    

    In your 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;
        }
        }