简体   繁体   English

如何在 Android 中的选项卡按钮之间添加空格?

[英]How to add space between tab buttons in Android?

I need to separate tab buttons with space, I tried to set margin to views and then add them as tabs, but it does not work, I also thought of adding empty view as divider, but haven't tried it yet, is there any standard way of doing this, or any tweak that can achieve same effect?我需要用空格分隔选项卡按钮,我尝试为视图设置边距,然后将它们添加为选项卡,但它不起作用,我也想过添加空视图作为分隔符,但还没有尝试过,有没有这样做的标准方式,或任何可以达到相同效果的调整?

Thanks!谢谢!

Here's the way:方法如下:

TabWidget tabWidget = (TabWidget) findViewById(android.R.id.tabs);
final int tabChildrenCount = tabWidget.getChildCount();
View currentView;
for (int i = 0; i < tabChildrenCount; i++) {
    currentView = tabWidget.getChildAt(i);
    LinearLayout.LayoutParams currentLayout =
        (LinearLayout.LayoutParams) currentView.getLayoutParams();
    currentLayout.setMargins(0, 5, 5, 0);
}
tabWidget.requestLayout();

This is really a good solution even for my problem, Many thanks for that.即使对于我的问题,这确实是一个很好的解决方案,非常感谢。 I used it to implement space before the first and after the last item in the widget to have the possibility to scroll them visible to the center without adding additional (and disturbing, because the widget does not excpect such silly things) invisible buttons.我用它在小部件中的第一个和最后一个项目之前和之后实现空间,以便可以将它们滚动到中心可见,而无需添加额外的(并且令人不安,因为小部件不期望这种愚蠢的东西)不可见按钮。

    //pump up space for first entry on the left and last entry on the right!
    Display display = getWindowManager().getDefaultDisplay();
    //Point size = new Point();
    int width = display.getWidth();

    View currentView = mTabHost.getTabWidget().getChildAt(0);
    LinearLayout.LayoutParams currentLayout = (LinearLayout.LayoutParams) currentView.getLayoutParams();
    currentLayout.setMargins(currentLayout.leftMargin + width/2, currentLayout.topMargin, currentLayout.rightMargin, currentLayout.bottomMargin);
    currentView = mTabHost.getTabWidget().getChildAt(mTabHost.getTabWidget().getChildCount()-1);
    currentLayout = (LinearLayout.LayoutParams) currentView.getLayoutParams();
    currentLayout.setMargins(currentLayout.leftMargin, currentLayout.topMargin, currentLayout.rightMargin  + width/2, currentLayout.bottomMargin);      
    mTabHost.getTabWidget().requestLayout();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM