【问题标题】:Android Tabs in middle of layout布局中间的 Android 选项卡
【发布时间】:2015-07-14 20:57:10
【问题描述】:

在我的布局中间,我希望有 2 个选项卡可以在屏幕后半部分查看 2 个不同的列表之间进行选择。我该怎么做?

这是一张图片,说明了我希望实现的目标。此图像在屏幕中间有一个问题选项卡和一个答案选项卡。根据您选择的选项卡,它会显示不同的列表视图:

我希望实现完全相同的目标。

我尝试使用 TabHost 小部件,但对于我来说,我无法摆脱标题栏(我尝试选择没有标题栏的主题,尝试将清单中的主题设置为无标题栏)

我也尝试过制作操作栏标签,但它们位于屏幕顶部....

如何在屏幕中间创建如图所示的选项卡?

【问题讨论】:

  • 您能否重新表述您的问题以更清楚地表达您的问题?
  • @Cookster 刚刚编辑了问题。基本上我想做的是在我的屏幕中间制作标签,但我不知道怎么做。我想请人解释一下。

标签: android android-layout android-tabhost android-tabs


【解决方案1】:

我今天也必须这样做!我尝试使用PagerTabStrip,因为我认为如果不是Toolbar,你就不能使用TabLayout。原来我错了,它甚至被用于Google iosched。所以你可以把TabLayout + ViewPager放在任何你想要的地方。

我知道你想做一些非常自定义的选项卡,如果它们是按钮会更容易自定义,但我认为最好使用 TabLayout + ViewPager,因为它使事情更具可扩展性。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <LinearLayout
        android:id="@+id/top_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal"></LinearLayout>

    <RelativeLayout
        android:id="@+id/bottom_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/top_layout"
        android:layout_weight="1">

        <android.support.design.widget.TabLayout
            android:id="@+id/pager_header"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:minHeight="60dp"
            app:tabGravity="fill"
            app:tabIndicatorColor="@color/colorAccentSecondary"
            app:tabMode="fixed"
            app:tabSelectedTextColor="@color/colorAccentSecondary"
            app:tabTextAppearance="@style/TournamentWaitingTimerTabTextAppearance" />

        <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/pager_header"></android.support.v4.view.ViewPager>
    </RelativeLayout>

</LinearLayout>

MainActivity 类:

public class MainActivity extends AppCompatActivity {

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

        // Locate the viewpager in activity_main.xml
        ViewPager viewPager = (ViewPager) findViewById(R.id.pager);

        // Set the ViewPagerAdapter into ViewPager
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new LeftFragment(), "Players");
        adapter.addFrag(new RightFragment(), "Prizes");

        viewPager.setAdapter(adapter);

        TabLayout mTabLayout = (TabLayout) findViewById(R.id.pager_header);
        mTabLayout.setupWithViewPager(viewPager);
    }

    class ViewPagerAdapter extends FragmentStatePagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFrag(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }
}

你可以更换你使用的适配器,没关系。

要自定义标签,我会看看this amazing tutorial

【讨论】:

  • 感谢回复,在我的活动中它显示标签栏但不显示片段内容,你能猜出是什么问题吗?
  • 片段内容显示为空,滑动操作不起作用:-(
【解决方案2】:

您可以尝试使用ViewPagerPagerTabStrip

您可以通过实现 FragmentPagerAdapter 将片段添加为页面选项卡

在您的布局 xml 文件中,您应该能够像大多数其他组件一样在 ViewPager 中移动到布局中的任何位置。我没有尝试将其居中,但我在其上方和下方放置了一些东西,它的行为与您预期的一样。

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
                                   android:id="@+id/pager"
                                   android:layout_width="match_parent"
                                   android:layout_height="match_parent" >

    <android.support.v4.view.PagerTabStrip
        android:id="@+id/pager_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"


</android.support.v4.view.ViewPager>

FragmentPagerAdapter 示例

 public class FragmentPagerAdapter extends android.support.v4.app.FragmentPagerAdapter
{
    final int PAGE_COUNT = 2;

    private final String[] PAGE_TITLES =
            {
                    "Fragment1",
                    "Fragment2"
            };

    public FragmentPagerAdapter()
    {
        super(getSupportFragmentManager());
    }

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

    @Override
    public CharSequence getPageTitle(int position)
    {
        return PAGE_TITLES[position];
    }

    @Override
    public Fragment getItem(int position)
    {
        switch(position)
        {
            case 0:
                return new Fragment1();
            case 1:
                return new Fragment2();
            default:
                return null;
        }

    }
}

在你的主要活动中:

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

...    
        mCustomPagerAdapter = new FragmentPagerAdapter();
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mCustomPagerAdapter);

...
    }

【讨论】:

  • 我试图复制您拥有的代码,但 getSupportFragmentManager() 似乎未定义。我在谷歌上搜索它,似乎你必须从 fragmentActivity 继承,但是我们已经扩展了 FragmentPageAdapter...
  • 好点...我从中获取代码的项目的 FragmentPagerAdapter 位于 MainActivity 的嵌套类中。尝试将类放在您希望使用它的活动中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多