質問

divider in tabWidget is working fine but when background of tab widget is set like

tabHost.getTabWidget().getChildTabViewAt(tabId).setBackgroundResource(R.drawable.tab_indicator);

problem is how to set divider in tab widget after setting background though i used

tabhost.getTabWidget().setDividerDrawable(R.drawable.tab_widget_divider);

is does not work for multiple tab.

役に立ちましたか?

解決

There may be multiple reasons for the dividers not showing up...

1. setDividerDrawable() must be used before you add the tabs to the tabHost to work.

2. By default there's are -2(left), -2(right) margins set to each tabindicator's View...

By default the system is using 9-patch drawables for tabWidget backgrounds with at least 2 pixels left transparent(or semi-transparent for shadow-effect) to the left and the right side.

my custom tab example

1. demonstration


original system 9-path drawable in android 2.3.3 (selected)enter image description here

2. Stock 9-path drawables for tab widget background. You can use these for experimenting


If you don't wan't to use 9-path drawables... you can set the margins to 0 to prevent the tab views overlapping your divider. Here's the code:

    View v;
    int count = tw.getTabCount();
    for (int i = 0; i < count; i++) {
        v = tw.getChildTabViewAt(i);
        v.setBackgroundResource(R.drawable.bg_tab);
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
        params.setMargins(0, 0, 0, 0);
    }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top