How to change color and / or pull TabWidget separator in Android?

I use TabLayout, and I have custom images for the tabs that I use, but for life I can't figure out how to change the color or even the image of the separator between the tabs and the contents of the tab. I tried using setDividerDrawable (), but it crashes when I call it before setting the contents of the tab, and just does nothing when I call it. If I can just get it black, which is enough, but so far nothing has worked. Thanks for any recommendations.

+5
source share
2 answers

You must do this:. TabHost.getTabWidget () setDividerDrawable (R.drawable.tab_divider);

Where R.drawable.tab_divider is the image in your resource directory.

But the key is that you have to do this BEFORE you add any tabs to the tab.

The initialization code for my bookmark looks like this:

private void initializeTabs(int curTab) {
    this.tabHost = getTabHost();
    tabHost.clearAllTabs();

    TabSpec ts1, ts2, ts3, ts4, ts5;
    // tab separator
    tabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);

    ts1 = this.setupTab(new TextView(this), tabHost, R.drawable.browse_tab_normal, 
            mResources.getString(R.string.Browse));

    ts2 = this.setupTab(new TextView(this), tabHost, R.drawable.search_tab_normal, 
            mResources.getString(R.string.Search));

    ts3 = this.setupTab(new TextView(this), tabHost, R.drawable.postad_tab_normal, 
            mResources.getString(R.string.Post));

    ts4 = this.setupTab(new TextView(this), tabHost, R.drawable.watchlist_tab_normal, 
            mResources.getString(R.string.WatchList));

    ts5 = this.setupTab(new TextView(this), tabHost, R.drawable.managead_tab_normal, 
            mResources.getString(R.string.Login));

    // intents
    ts1.setContent(new Intent().setClass(this, BrowseTabActivity.class));
    ts2.setContent(new Intent().setClass(this, SearchTabActivity.class));
    ts3.setContent(new Intent().setClass(this, PostAdTabActivity.class));
    ts4.setContent(new Intent().setClass(this, WatchlistTabActivity.class));
    ts5.setContent(new Intent().setClass(this, LoginTabActivity.class));

    tabHost.addTab(ts1);
    tabHost.addTab(ts2);
    tabHost.addTab(ts3);
    tabHost.addTab(ts4);
    tabHost.addTab(ts5);

...

+9
source

The best way to define a delimiter is to do if from your XML markup:

<TabWidget
     android:layout_width="match_parent"
     android:showDividers="middle"
     android:divider="@drawable/design_tab_divider">
 </TabWidget>

Thus, you can determine the ability to pull only from the markup. Keep in mind that you must use android:dividerwith android:showDividers="middle"to place separators between tabs. Read more in the specification and pay attention to the properties inherited from LinearLayout - docs in google

0
source

All Articles