I'm using a TabLayout and I have custom images for the tabs that I am using, but for the life of me I can't figure out how to change the color or even the image of the divider between the tabs and the tab content. I have attempted to use setDividerDrawable(), but it crashes when I call it before setting the tab content and just does nothing when I call it after. If I can just get it to be black that would be sufficient, but so far nothing has worked. Thanks for any guidance.
我正在使用TabLayout,我有自定义图像用于我正在使用的选项卡,但对于我的生活,我无法弄清楚如何更改选项卡和选项卡内容之间的分隔符的颜色甚至图像。我试图使用setDividerDrawable(),但是在我设置选项卡内容之前调用它时它会崩溃,而在我之后调用它时什么都不做。如果我能把它变成黑色就足够了,但到目前为止还没有任何效果。感谢您的指导。
2 个解决方案
#1
9
You have to do this: tabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
你必须这样做:tabHost.getTabWidget()。setDividerDrawable(R.drawable.tab_divider);
Where R.drawable.tab_divider is an image in your resources directory.
R.drawable.tab_divider是资源目录中的图像。
But the key is you have to do that BEFORE you've added any tabs to the tab host.
但关键是你必须在向标签主机添加任何标签之前这样做。
My tab initialization code looks like:
我的标签初始化代码如下:
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);
...
...
#2
0
The better way to define a divider is to make if from your XML markup:
定义分隔符的更好方法是从XML标记中创建:
<TabWidget
android:layout_width="match_parent"
android:showDividers="middle"
android:divider="@drawable/design_tab_divider">
</TabWidget>
So, you can define a drawable just from the markup. Mind that you must use android:divider
along with android:showDividers="middle"
to place the dividers between tabs. For more read the spec and pay attention to properties inherited from LinearLayout - docs in google
因此,您可以从标记中定义一个drawable。请记住,你必须使用android:divider和android:showDividers =“middle”将分隔符放在标签之间。如需更多阅读规范并注意从LinearLayout继承的属性 - 谷歌中的文档
#1
9
You have to do this: tabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
你必须这样做:tabHost.getTabWidget()。setDividerDrawable(R.drawable.tab_divider);
Where R.drawable.tab_divider is an image in your resources directory.
R.drawable.tab_divider是资源目录中的图像。
But the key is you have to do that BEFORE you've added any tabs to the tab host.
但关键是你必须在向标签主机添加任何标签之前这样做。
My tab initialization code looks like:
我的标签初始化代码如下:
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);
...
...
#2
0
The better way to define a divider is to make if from your XML markup:
定义分隔符的更好方法是从XML标记中创建:
<TabWidget
android:layout_width="match_parent"
android:showDividers="middle"
android:divider="@drawable/design_tab_divider">
</TabWidget>
So, you can define a drawable just from the markup. Mind that you must use android:divider
along with android:showDividers="middle"
to place the dividers between tabs. For more read the spec and pay attention to properties inherited from LinearLayout - docs in google
因此,您可以从标记中定义一个drawable。请记住,你必须使用android:divider和android:showDividers =“middle”将分隔符放在标签之间。如需更多阅读规范并注意从LinearLayout继承的属性 - 谷歌中的文档