问题描述
是否可以在TabLayout中的选项卡之间添加边距?我尝试对 Widget.Design.TabLayout 使用自定义样式,但是有些属性仅与填充相关,而没有边距.
Is there a way to add margin between the tabs in a TabLayout? I've tried with using a custom style for Widget.Design.TabLayout, but there are properties only related to padding, but no margins.
推荐答案
好吧,在花了2-3个小时之后,我终于找到了解决方案.
Ok mates, after spending 2-3 hours on that I finally found a solution.
如果使用的是TabLayout,则无法使用样式等在选项卡上添加边距. (就像@Connecting life与Android一样)
If you are using TabLayout there is no way to add margins to the tabs by using styles and so on. (as @Connecting life with Android earlier)
但是,您可以通过编写一些Java代码来做到这一点.总的来说,您的代码应与该代码相似:
But, you can do that by writing some Java code. All in all your code should look similar to that one:
for(int i=0; i < mTabLayout.getTabCount(); i++) {
View tab = ((ViewGroup) mTabLayout.getChildAt(0)).getChildAt(i);
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) tab.getLayoutParams();
p.setMargins(0, 0, 50, 0);
tab.requestLayout();
}
为了将每个选项卡作为一个视图,我们必须首先获取包含它们的容器.在这种情况下, TabLayout 使用 SlidingTabStrip 作为标签的容器. SlidingTabStrip是TabLayout的第一个子代:
In order to get each and every tab as a View we have to first get the container which contains them. In this case the TabLayout is using a SlidingTabStrip as a container for the tabs. The SlidingTabStrip is the first child of the TabLayout:
View tab = ((ViewGroup) mTabLayout.getChildAt(0))
在完成了这些小细节之后,一切都变得很简单.
And after this small detail, everything is pretty straight forward.
这篇关于如何在TabLayout中的选项卡之间添加边距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!