Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 607
hzxu Avatar asked Nov 29 '22 04:11

hzxu


2 Answers

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();
like image 117
Kamen Avatar answered Dec 09 '22 22:12

Kamen


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();
like image 31
Riffer Avatar answered Dec 09 '22 23:12

Riffer