布局中间的 Android 选项卡

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

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

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

TabExample

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

我尝试使用 TabHost 小部件来完成它,但对于我的生活我无法摆脱标题栏(我尝试选择没有标题栏的主题,尝试将 list 中的主题设置为无标题栏)

我也试过制作操作栏标签,但那些在屏幕的顶部....

如何像图片中那样在屏幕中间创建标签页?

最佳答案

我今天也必须这样做!我尝试使用 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 .

关于布局中间的 Android 选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31417209/

相关文章:

java - Android 选项卡 View

android - 将android背景设置为中心裁剪,但右对齐?

java - Android 警报对话框 - 退出应用程序

android - 操作栏和更改图标

安卓 : set visibility of individual buttons in a custom ListView

Android 如何从 TabHost Activity 中将额外内容添加到 Activity 中?

java - MPAndroidChart 没有更新

android - AlertDialog setContentView 不工作 API22

android - 在 ConstraintLayout 中使用 <include> 和 <merge>

android - 在选项卡之间传递数据