SlidingTabLayout to fit the screen

前端 未结 9 1655
南笙
南笙 2020-12-07 18:45

I am using Google\'s SlidingTabLayout. As i only have 3 tabs to be displayed, I am seeing empty space after last tab. Please screenshot below.

9条回答
  •  再見小時候
    2020-12-07 19:13

    more dynamic approach is

    Edit SlidingTabLayout.java

    locate private void populateTabStrip() method and replace

    mTabStrip.addView(tabView);
    

    with

     // get dimension of current layout
     DisplayMetrics display = this.getResources().getDisplayMetrics();
       int width = display.widthPixels;
            //your tab names here
            String[] tabTitles = {"tab1", "tab2", "tab3"}
            // compare max width and number of tabs's length 
            //will not fit to the maxwidth it will distribute the tabs evenly
            if(width%(width/tabTitles.length) == 0){
                mTabStrip.addView(tabView,
                        new LinearLayout.LayoutParams(width/tabTitles.length, ViewGroup.LayoutParams.WRAP_CONTENT));
            } else {
                mTabStrip.addView(tabView);
            }
    

提交回复
热议问题