Android的标签布局的中间 [英] Android Tabs in middle of layout

查看:227
本文介绍了Android的标签布局的中间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的布局中间我想有2个选项卡在屏幕下半年观看2个不同的列表之间进行选择。我怎样才能做到这一点?

In the middle of my layout I want to have 2 tabs to choose between viewing 2 different lists in the second half of the screen. How can I do this?

下面是说明什么,我希望实现的图像。此图片有问题选项卡,并在屏幕中间的答案选项卡。根据该选项卡中,选择它显示了一个不同的列表视图:

Here is an image illustrating what I wish to achieve. This image has a questions tab and an answers tab in the middle of the screen. Depending on which tab you select it shows a different listview:

我想实现这个确切的同样的事情。

I wish to achieve this exact same thing.

我试着用TabHost部件做,但我的生活我无法摆脱的标题栏(我试过没有标题栏选择主题,试图在清单中的主题设置为一个没有标题列中的一)

I tried doing it with TabHost widget, but for the life of me I couldn't get rid of the title bar (I tried selecting themes with no title bar, tried setting the theme in the manifest to a no title bar one)

我也试过化妆操作栏中的标签,但这些都是在屏幕的顶部....

I also tried make action bar tabs, but those were at the top of the screen....

我如何创建我的屏幕中间的标签,如图像?

How can I create tabs in the middle of my screen like in the image?

推荐答案

您可以尝试使用的 ViewPager PagerTabStrip

您可以通过实现FragmentPagerAdapter添加片段作为页面标签

You can add fragments as page tabs by implementing a FragmentPagerAdapter

在您的布局xml文件,你应该能够围绕ViewPager任何地方在布局移动到像你最有其他组件。我没有试过定心,但我已经把上面和下面的东西,它的行为如你所愿。

In your layout xml file, you should be able to move around the ViewPager to anywhere in your layout like you would most other components. I haven't tried centering it, but I have placed things above and below it, and it behaves as you would expect.

<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例如

FragmentPagerAdapter example

 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;
        }

    }
}

和在主Acivity:

and in your main Acivity:

    @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);

...
    }

这篇关于Android的标签布局的中间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆