Android如何将TabLayout定位到屏幕底部(适用于谷歌的材料设计库TabLayout)
我试图使用一个与ViewPager结合的表格布局来创建三个可切换的片段。但是,我遇到了一个问题:我希望TabLayout位于屏幕底部,但是ViewPager强制它位于顶部 TabLayout过去位于android.support.design.widget库中。然而,自从引入androidx以来,这个库已经发生了变化。新库是com.google.android.material.tabs.TabLayout 以前的(和过时的)帖子(,)建议通过简单地创建垂直Android如何将TabLayout定位到屏幕底部(适用于谷歌的材料设计库TabLayout),android,android-fragments,android-viewpager,android-tablayout,Android,Android Fragments,Android Viewpager,Android Tablayout,我试图使用一个与ViewPager结合的表格布局来创建三个可切换的片段。但是,我遇到了一个问题:我希望TabLayout位于屏幕底部,但是ViewPager强制它位于顶部 TabLayout过去位于android.support.design.widget库中。然而,自从引入androidx以来,这个库已经发生了变化。新库是com.google.android.material.tabs.TabLayout 以前的(和过时的)帖子(,)建议通过简单地创建垂直LinearLayout并将ViewP
LinearLayout
并将ViewPager
和TabLayout
指定为该LinearLayout
的两个独立元素,来简单解决上述问题
上述解决方案不再可行,因为现在必须将TabLayout
指定为ViewPager
()内部的元素,如下所示:
<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>
现在,我如何通过xml代码或编程方式将TabLayout整齐地放在ViewPager的底部?根据您的参考-要在ViewPager中包含TabLayout,请在ViewPager元素中添加TabLayout元素 但这并不意味着不能使用TabLayout outside ViewPager。 所以你可以使用以前文章中的任何答案 或者你可以这样试试
<?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>
我找到了一个答案:simpy将
android:layout\u gravity=“bottom”
添加到xml格式的表格布局中
因此,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>
找到了一个更简单的解决方案。。。检查我的答案:D的可能副本
<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>