【问题标题】:Android how to position TabLayout to the bottom of the screen (for Google's material design library TabLayout)Android如何将TabLayout定位到屏幕底部(针对谷歌的材料设计库TabLayout)
【发布时间】:2019-09-23 07:32:35
【问题描述】:

我正在尝试使用 TabLayout 与 ViewPager 连接来创建三个可滑动的片段。但是,我遇到了一个问题:我希望 TabLayout 位于屏幕底部,但 ViewPager 却强制它位于顶部

TabLayout 曾经位于 android.support.design.widget 库中。然而,自从引入了 androidx 以来,这个库已经发生了变化。新库是 com.google.android.material.tabs.TabLayout

以前的(和过时的)帖子(example1example2)建议通过简单地创建一个垂直的LinearLayout 并将ViewPagerTabLayout 指定为其中的两个独立元素来解决上述问题LinearLayout.

上述解决方案不再可行,因为现在您必须将TabLayout 指定为ViewPager (reference) 内部的一个元素,如下所示:

<androidx.viewpager.widget.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        >
    </com.google.android.material.tabs.TabLayout>
</androidx.viewpager.widget.ViewPager>

然后,要将ViewPagerTabLayout“合并”,必须在TabLayout 实例上调用setupWithViewPager(ViewPager vp) 方法:

ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
MainPagerAdapter mpa = new MainPagerAdapter(getSupportFragmentManager(), 3);
viewPager.setAdapter(mpa);

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

现在,我怎样才能通过 xml 代码或以编程方式巧妙地将 TabLayout 置于 ViewPager 的底部?

【问题讨论】:

标签: android android-fragments android-viewpager android-tablayout


【解决方案1】:

我找到了答案:简单地将android:layout_gravity="bottom" 添加到 xml 中的TabLayout

所以 xml 实现现在看起来像:

<androidx.viewpager.widget.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_gravity="bottom"
            >
        </com.google.android.material.tabs.TabLayout>
    </androidx.viewpager.widget.ViewPager>

【讨论】:

    【解决方案2】:

    根据您的参考 - 要在 ViewPager 中包含 TabLayout,请在 ViewPager 元素内添加 TabLayout 元素。

    但这并不意味着您不能在 ViewPager 之外使用 TabLayout。 因此您可以使用以前帖子中的任何答案 或者你可以这样尝试

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    <androidx.viewpager.widget.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/tab_layout"/>
    
    <com.google.android.material.tabs.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_height="48dp"/></RelativeLayout>
    

    【讨论】:

    • 找到一个更简单的解决方案...检查我的答案:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    • 2015-08-12
    • 2021-05-23
    • 2015-11-16
    • 1970-01-01
    相关资源
    最近更新 更多