【问题标题】:Multiline PageTitle with android.support.design.widget.TabLayout带有 android.support.design.widget.TabLayout 的多行 PageTitle
【发布时间】:2017-12-08 10:34:57
【问题描述】:

在一个 android 应用程序中,我使用 TabLayout 在单个活动中显示多个 Fragments。 我希望标签带有图标和两行标题。

在 Activity 的 onCreate 中,我初始化了 SectionsPageAdapterTabLayout,例如:

private SectionsPagerAdapter mSectionsPagerAdapter;
protected void onCreate(Bundle savedInstanceState) {
    ...
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new 
    SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    // add a PageChangeListener to be able to detect which tab is activated
    mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){
        ...
    });

    ...
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);
    tabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);

    //Set icons for tabs
    tabLayout.getTabAt(0).setIcon(R.drawable.ic0);
    tabLayout.getTabAt(1).setIcon(R.drawable.ic1);
    tabLayout.getTabAt(2).setIcon(R.drawable.ic2);
}

SectionpagerAdapter 中我定义了 PageTitles 使用:

public CharSequence getPageTitle(int position) {
    switch (position) {
        case 0:
            return "First \nTab";
        case 1:
            return "Second \nTab";
        case 2:
            return "Third \nTab";
    }
    return null;
}

但是当我运行应用程序时,PageTitles 并没有分成两行,而是用 ... 缩短以适应屏幕。

如何为 Tabs 启用多行 PageTitles

【问题讨论】:

    标签: android android-fragments android-tablayout


    【解决方案1】:
    tabLayout.getTabAt(i).setCustomView(yuorTabView);
    

    你可以自定义你自己的 TabView

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-01
      • 2019-09-01
      • 2014-01-21
      • 2015-10-21
      • 2023-03-24
      相关资源
      最近更新 更多