2011-05-26 129 views
4

我需要用空格分隔标签按钮,我试图设置边缘视图,然后将它们添加为标签,但它不起作用,我也想添加空视图作为分隔线,但还没有尝试过,是有没有任何标准的做法,或任何调整,可以实现相同的效果?如何在Android中的标签按钮之间添加空间?

谢谢!

回答

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

谢谢,我会试试看。 – hzxu 2011-05-26 20:58:25

+0

我试过了,它确实有用,谢谢! – hzxu 2011-05-26 23:08:47

+0

@Kamen:谢谢......它工作完美! :) +1 – Hiral 2012-03-02 07:33:59

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(); 
相关问题