ogaclejapan / SmartTabLayout

A custom ViewPager title strip which gives continuous feedback to the user when scrolling
Apache License 2.0
7.09k stars 1.34k forks source link

Specify "weight" for tabs #78

Closed vipulyaara closed 9 years ago

vipulyaara commented 9 years ago

Hi. The view is very very flexible and I have used it for some very unique combinations. I wonder if I can make tabs of different sizes, while still filling up the screen.

I want 3 tabs. First and last of them are icons, and second is a long text. So I want to provide secondtab twice as weight of first and third. Currently it looks like the one in picture. screenshot_2015-10-06-11-37-54

ogaclejapan commented 9 years ago

HI, @vipulyaara

It works :) Without using DistributeEvenly, to specify the weight in a custom layout

  smartTabLayout.setDistributeEvenly(false);
  smartTabLayout.setCustomTabView(new SmartTabLayout.TabProvider() {
    @Override
    public View createTabView(ViewGroup container, int position, PagerAdapter adapter) {

      View view;
      switch (position) {
        case 0:
          view = inflater.inflate(R.layout.custom_tab_icon, container, false);
          break;
        case 1:
          view = inflater.inflate(R.layout.custom_tab_text, container, false);
          break;
        case 2:
          view = inflater.inflate(R.layout.custom_tab_icon, container, false);
          break;
        default:
          throw new IllegalStateException("Invalid position: " + position);
      }
      return view;
    }
  });

  smartTabLayout.setViewPager(...);

layout/custom_tab_icon.xml


<?xml version="1.0" encoding="utf-8"?>
<ImageView
  xmlns:android="http://schemas.android.com/apk/res/android"
  ...
  android:layout_width="0dp"
  android:layout_height="match_parent"
  android:layout_weight="1"
  ...
  />

layout/custom_tab_text.xml


<?xml version="1.0" encoding="utf-8"?>
<TextView
  xmlns:android="http://schemas.android.com/apk/res/android"
  ...
  android:layout_width="0dp"
  android:layout_height="match_parent"
  android:layout_weight="2"
  ...
  />
vipulyaara commented 9 years ago

Great. It works. Thanks.