屏幕底部的TabLayout位置 [英] TabLayout position at the bottom of the screen

查看:175
本文介绍了屏幕底部的TabLayout位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 android.support.design.widget ,我需要在 activity_main.xml 中设置 TabLayout 的位置code>在屏幕底部

I'm using android.support.design.widget and I need to set the position of TabLayout at the activity_main.xml in the bottom of the screen.

我已经看到您可以使用TabHost,但我更喜欢使用 android.support.design

I've seen that you can use TabHost, but I prefer to use android.support.design

ativity_main.xml

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="@dimen/custom_tab_layout_height"
        app:tabMode="fixed"
        app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

MainActivity.java

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

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);


    TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
    tabOne.setText("ONE");
    tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_tab_call, 0, 0);
    tabLayout.getTabAt(0).setCustomView(tabOne);


    TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
    tabTwo.setText("TWO");
    tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.iconos, 0, 0);
    tabLayout.getTabAt(1).setCustomView(tabTwo);

}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFrag(new OneFragment(), "");
    adapter.addFrag(new TwoFragment(), "");
    adapter.addFrag(new ThreeFragment(), "");
    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    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);
    }
}

有什么想法吗?
谢谢。

Any ideas? Thanks.

推荐答案

我的Tablayout位于LinearLayout内,如下图所示,给出了linearlayout的底部 tp重力,这就是我在班级点击此处

My Tablayout is inside a LinearLayout as you see below, gave "bottom" tp gravity of linearlayout, and this is how I use it in my class click here

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="460dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_gravity="bottom"
        android:gravity="center">

        <android.support.design.widget.TabLayout
            android:id="@+id/tablayout_home"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            app:tabGravity="fill"
            app:tabMode="fixed" />


    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

这篇关于屏幕底部的TabLayout位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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