Android TabWidgets 之间的分隔线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3705346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:27:48  来源:igfitidea点击:

dividers between TabWidgets

androidlayouttabstabwidgetdivider

提问by mbethdev

Is the android:divider attribute under the TabWidget working? I tried the Tab Layout tutorial from android just to test (http://developer.android.com/resources/tutorials/views/hello-tabwidget.html) and set the android:divider to some image (for now I used the android vertical scrollbar as the drawable to really emphasize if its getting picked up (copied it from frameworks), but when I ran it on the emulator, it doesn't appear to be working. According to the docs, the TabWidget does seem to support this attribute: "Drawable used to draw the divider between tabs."

TabWidget 下的 android:divider 属性是否有效?我尝试了来自 android 的 Tab Layout 教程只是为了测试(http://developer.android.com/resources/tutorials/views/hello-tabwidget.html)并将 android:divider 设置为一些图像(现在我使用了 android垂直滚动条作为可绘制对象来真正强调它是否被拾取(从框架复制),但是当我在模拟器上运行它时,它似乎不起作用。根据文档,TabWidget 似乎确实支持这一点属性:“可绘制用于绘制选项卡之间的分隔线。”

Can anyone help? I am using a nine-patched drawable as my divider image drawable.

任何人都可以帮忙吗?我使用九个补丁可绘制作为我的分隔图像可绘制。

MB

MB

回答by Josh Clemm

It doesn't look like the dividerattribute is available anymore for TabWidget. One way to add a custom divider is to set it programmatically:

看起来该divider属性不再适用于TabWidget. 添加自定义分隔符的一种方法是以编程方式设置它:

mTabHost.getTabWidget().setDividerDrawable(R.drawable.divider_vertical_dark);

Make sure however, you call this beforeyou set the content of the tabs. It would crash on me if I called it after.

但是,请确保设置选项卡内容之前调用它。如果我在之后调用它,它会在我身上崩溃。

回答by DanKodi

I had the problem in ICS, where divider was visible. None of the solutions worked except for the following.

我在 ICS 中遇到了问题,在那里可以看到分隔线。除了以下解决方案外,没有任何解决方案有效。

<TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:gravity="bottom"
            android:layout_alignParentBottom="true"
            android:fadingEdge="none"
            android:showDividers="none" >
        </TabWidget>

Key line was android:showDividers="none"

关键线是 android:showDividers="none"

回答by Ramyavjr

I had this issue and solved it with the following code

我遇到了这个问题并使用以下代码解决了它

tabHost1.getTabWidget().setDividerDrawable(R.drawable.example1);
if(Build.VERSION.SDK_INT >= 11)
    tabHost1.getTabWidget().setShowDividers(TabWidget.SHOW_DIVIDER_MIDDLE);

For api levels below 11, it worked with the first line. For 11 and higher I included this to get this working. setShowDividers is added in linearlayout from api level 11. Hope this helps someone

对于低于 11 的 api 级别,它适用于第一行。对于 11 及更高版本,我将其包含在内以使其正常工作。setShowDividers 从 API 级别 11 开始添加到 linearlayout 中。希望这对某人有所帮助

回答by Squall

Having the same issue myself. I only see the problem in Ice Cream Sandwich (ICS / 4.0.x). In android 1.6 - 2.3.4 there is no issue, dividers show up properly when setting a drawable in code, or in the xml layout.

我自己也有同样的问题。我只在 Ice Cream Sandwich (ICS / 4.0.x) 中看到了这个问题。在 android 1.6 - 2.3.4 中没有问题,在代码或 xml 布局中设置可绘制对象时,分隔符正确显示。

I've tried just about everything I can think of to fix it but nothing works, including Josh's answer above :( though I have noticed that when setting any drawable as the divider, it will take up the space between tabs as if there was a drawable there, but it's just not visible.

我已经尝试了我能想到的所有方法来修复它,但没有任何效果,包括上面乔希的回答:(尽管我注意到将任何可绘制对象设置为分隔符时,它会占用标签之间的空间,就好像有一个在那里可以绘制,但它只是不可见。

Hopefully that gives someone else a hint as to what could be happening..?

希望这能给其他人一个关于可能发生什么的提示..?

回答by Squall

I removed divider line from tabbar with use of below magical lines.

我使用下面的神奇线条从标签栏中删除了分隔线。

  mTabHost.getTabWidget().setDividerDrawable(null);

OR

或者

  mTabHost.getTabWidget().setDividerDrawable(R.Color.transperant);