SlidingTabLayout to fit the screen

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

    You can create a custom tab layout and assign it to your SlidingTabLayout to inflate.

    custom_tab.xml:

    
      
    
    

    In your code:

        viewPager = (ViewPager) rootView.findViewById(R.id.viewPager);
        viewPager.setAdapter(new SampleAdapter(getChildFragmentManager()));
        SlidingTabLayout tabLayout = (SlidingTabLayout)rootView.findViewById(R.id.slidingTab);
        tabLayout.setCustomTabView(R.layout.custom_tab,R.id.customText);
        tabLayout.setViewPager(viewPager);
    

    By keeping the width to 0 dp and the weight as 1 (the number is arbitrary), the layout should take care of giving all your tabs equal screen space. This should also work for any number of tabs. You shouldn't need to modify your SlidingTabLayout.java.

提交回复
热议问题