SlidingTabLayout to fit the screen

前端 未结 9 1678
南笙
南笙 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:14

    In SlidingTabLayout.java, find

    protected TextView createDefaultTabView(Context context)
    

    and add these lines to that method:

    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    textView.setWidth(size.x / 3);
    

    That fixed it for me. Basically, you're getting the width of your screen and dividing by 3 (the number of tabs) and setting that as the width for the TextField that makes up your tab.

    UPDATE:

    In the current source, google added a setDistributeEvenly(Boolean); method, so you can use slidingTabs.setDistributeEvenly(true); instead. When using the older source code, either use my solution above, or update to the newest source (latter is recommended).

提交回复
热议问题