【问题标题】:Why does setDistributeEvenly fail when using a custom tab layout within a SlidingTabLayout?为什么在 SlidingTabLayout 中使用自定义选项卡布局时 setDistributeEvenly 会失败?
【发布时间】:2016-03-15 18:12:26
【问题描述】:

我将 SlidingTabLayout 与我自己的自定义选项卡布局一起使用。一切都按预期工作;选项卡正确显示(标题、顺序、颜色等),但是 setDistributeEvenly 在这种情况下不起作用。措辞较长的选项卡比措辞较短的选项卡具有更长的选择指示符。例如,“篮球”的指示符比“高尔夫”长得多。这是我的代码。如您所见,setDistributeEvenly 设置为 true。

    ViewPager pager;
    ViewPagerAdapter adapter;
    SlidingTabLayout tabs;

    adapter =  new ViewPagerAdapter(getSupportFragmentManager(), 10, this);

    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);

    tabs = (SlidingTabLayout) findViewById(R.id.tab_layout);
    tabs.setCustomTabView(R.layout.custom_tab, R.id.textView2);
    tabs.setDistributeEvenly(true);
    tabs.setBackgroundColor(getResources().getColor(R.color.red));
    tabs.setSelectedIndicatorColors(getResources().getColor(R.color.white));
    tabs.setViewPager(pager);

这是我的自定义布局:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    android:id="@+id/tab_title"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="8dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="8dp"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="#ffffff"/>

我尝试将布局宽度和高度设置为 match_parent、wrap_content 和不同“dp”的各种组合,但也无济于事。您在上面看到的填充不会改变指标的长度。

您对为什么 setDistributeEvenly(true) 在这种情况下会失败有任何想法吗?

【问题讨论】:

    标签: android android-layout tabs android-custom-view


    【解决方案1】:

    你应该设置tabLayout的重力:

    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    

    并且不要将您的 tabLayout 设置为可滚动(app:tabMode="scrollable"),否则重力填充将不起作用。检查这个 Link

    【讨论】:

    • 我使用的是 Google 的 SlidingTabLayout 而不是简单的 TabLayout。这有什么改变吗? SlidingTabLayout 不会立即提供 TabLayout。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-09
    • 1970-01-01
    • 2014-10-23
    • 1970-01-01
    • 2018-06-23
    相关资源
    最近更新 更多