Android一个选项卡TabLayout无法完全展开的问题

13

我想在TabLayout中添加一个全宽指示器的选项卡,但使用Google TabLayout时,结果不是我期望的效果google tab layout

之后,我使用了相同的代码与PagerSlidingTabStrip库,并获得了正确的结果PagerSlidingTabStrip library

有人之前遇到过同样的问题吗?如何解决?

我只使用了一些简单的活动来实现上述效果。

<android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/swipeAppBarLayout"
        >

        <android.support.design.widget.TabLayout
            android:id="@+id/swipeTabLayout"
            android:layout_width="match_parent"
            app:tabMaxWidth="0dp"
            android:layout_height="30dp"
            app:tabIndicatorHeight="4dp"
            />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/swipePager"
        android:background="@android:color/transparent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

和适配器

public class StreamNavigationPagerAdapter extends FragmentStatePagerAdapter {

    private Context mContext;

    public StreamNavigationPagerAdapter(FragmentManager fm, Context con) {
        super(fm);
        mContext = con;
    }


    @Override
    public Fragment getItem(int position) {
        return getNewsFragment();
    }

    private Fragment getNewsFragment() {
        Fragment fragment = new StreamNewsFragment();
        return fragment;
    }

    @Override
    public int getCount() {
            return 1;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return "NEWS";
    }

}

the activity contain just

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ButterKnife.inject(this);

            swipePager.setAdapter(new StreamNavigationPagerAdapter(getSupportFragmentManager(), this));
            swipeTabLayout.setupWithViewPager(swipePager);
    //        swipeTabLayout.setViewPager(swipePager);
        }

可能是TabLayout中的单个选项卡未覆盖整个宽度的重复问题。 - carpinchosaurio
1个回答

34

我遇到了完全相同的问题,并通过将 tabMaxWidth 属性设置为一个高值(例如 500dp),而不是一些答案中建议的 0dp,来解决它。

我也遇到了类似的问题,并且通过将 tabMaxWidth 属性设为一个较大的值(如500dp),而不是像某些答案中建议的设置为0dp来解决它。

<android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMaxWidth="500dp" />

在此输入图片描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接