如何在Android中的标签按钮之间添加空格?

时间:2011-05-26 13:23:01

标签: android android-layout

我需要用空格分隔标签按钮,我试图将边距设置为视图,然后将它们添加为标签,但它不起作用,我还考虑将空视图添加为分隔符,但还没有尝试过,有没有任何标准的方法,或任何可以达到同样效果的调整?

谢谢!

2 个答案:

答案 0 :(得分:21)

这是方式:

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();

答案 1 :(得分:1)

即使对我的问题,这也是一个很好的解决方案!非常感谢!我用它在小部件中的第一个和最后一个项目之前实现空间,以便可以将它们可见地滚动到中心而不添加额外的(并且令人不安,因为小部件不会检测到这些愚蠢的东西)隐形按钮。

    //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();