如何在屏幕底部设置androidTabLayout?
How to set android TabLayout in the bottom of the screen?
我的问题是如何将新的 android material 设计 TabLayout 设置在屏幕底部,有点像 Instagram 的底部工具栏。
如果您从未见过 Instagram 的 UI,这里是它的屏幕截图:
。如果有更好的方法来解决这个问题,请随时 post 在这里(如果可能的话附上代码示例),我将不胜感激。
这是我的代码:activity_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.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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" />
我已经尝试了 Stack Overflow 社区建议的许多方法和解决方法,但 none 似乎适用于 android 中的这个新选项卡实现。我知道这个 UI 设计不遵循 android 设计指南,所以请不要评论它。 UI 设计对我的应用程序的用户体验至关重要,如果能得到答案,我将不胜感激。谢谢!
我相信我有最简单的修复方法。使用 LinearLayout 并将 viewpager 的高度设置为 0dp,layout_weight="1"。确保 LinearLayout 的方向为垂直,并且 viewpager 位于 TabLayout 之前。这是地雷的样子:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<include layout="@layout/toolbar" android:id="@+id/main_toolbar"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/white"/>
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blue"
/>
</LinearLayout>
作为奖励,您应该只创建一次工具栏 toolbar.xml。所以你所要做的就是使用 include 标签。使您的布局更干净。享受吧!
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
2016 年 2 月 11 日更新: 对于那些不知道如何膨胀工具栏的人,这里是方法。确保您的 Activity 扩展了 AppCompatActivity 以便您可以调用 setSupportActionBar() 和 getSupportActionBar()。
Toolbar mToolbar = (Toolbar) findViewById(R.id.main_toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
用这个
替换你的 TabLayout 代码
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:elevation="2dp" />
我在 Android Studio 2.2 上遇到了同样的问题。这就是我所做的,
<?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:orientation="vertical"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:subtitleTextColor="@color/color_white"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:title="@string/call_log"
app:titleTextColor="@color/color_white"
/>
<RelativeLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/tabLayout" />
<android.support.design.widget.TabLayout
android:layout_alignParentBottom="true"
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"/>
</RelativeLayout>
</LinearLayout>
最好像我下面的代码那样将 AppBarLayout 和 tabLayout 分开。这样您就可以独立修改标签栏和查看分页器属性。
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<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"
android:layout_above="@+id/tabs"/>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
android:layout_alignParentBottom="true"
在
中添加此设置
android.support.design.widget.TabLayout
页面顶部LinearLayout设置和设置android:gravity="bottom"
。而已。这是代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px"
android:gravity="bottom"> //Thats it.
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:id="@+id/tabLayout1">
<android.support.design.widget.TabItem
android:icon="@drawable/home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabItem1" />
<android.support.design.widget.TabItem
android:icon="@drawable/mypage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabItem2" />
<android.support.design.widget.TabItem
android:icon="@drawable/friends"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabItem3" />
<android.support.design.widget.TabItem
android:icon="@drawable/messages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabItem4" />
<android.support.design.widget.TabItem
android:icon="@drawable/settings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabItem5" />
</android.support.design.widget.TabLayout>
</LinearLayout>
我建议使用 androidx 库,因为 android 支持库可能很快就会被弃用。
我们实际上可以向 viewpager 添加选项卡布局。下面是我在项目中使用的代码片段
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00a294"
android:layout_gravity="bottom" />
</androidx.viewpager.widget.ViewPager>
layout_gravity="bottom"是把tab布局放到最下面的语法
我的问题是如何将新的 android material 设计 TabLayout 设置在屏幕底部,有点像 Instagram 的底部工具栏。
如果您从未见过 Instagram 的 UI,这里是它的屏幕截图:
这是我的代码:activity_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.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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" />
我已经尝试了 Stack Overflow 社区建议的许多方法和解决方法,但 none 似乎适用于 android 中的这个新选项卡实现。我知道这个 UI 设计不遵循 android 设计指南,所以请不要评论它。 UI 设计对我的应用程序的用户体验至关重要,如果能得到答案,我将不胜感激。谢谢!
我相信我有最简单的修复方法。使用 LinearLayout 并将 viewpager 的高度设置为 0dp,layout_weight="1"。确保 LinearLayout 的方向为垂直,并且 viewpager 位于 TabLayout 之前。这是地雷的样子:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<include layout="@layout/toolbar" android:id="@+id/main_toolbar"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/white"/>
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blue"
/>
</LinearLayout>
作为奖励,您应该只创建一次工具栏 toolbar.xml。所以你所要做的就是使用 include 标签。使您的布局更干净。享受吧!
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
2016 年 2 月 11 日更新: 对于那些不知道如何膨胀工具栏的人,这里是方法。确保您的 Activity 扩展了 AppCompatActivity 以便您可以调用 setSupportActionBar() 和 getSupportActionBar()。
Toolbar mToolbar = (Toolbar) findViewById(R.id.main_toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
用这个
替换你的 TabLayout 代码<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:elevation="2dp" />
我在 Android Studio 2.2 上遇到了同样的问题。这就是我所做的,
<?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:orientation="vertical"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:subtitleTextColor="@color/color_white"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:title="@string/call_log"
app:titleTextColor="@color/color_white"
/>
<RelativeLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/tabLayout" />
<android.support.design.widget.TabLayout
android:layout_alignParentBottom="true"
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"/>
</RelativeLayout>
</LinearLayout>
最好像我下面的代码那样将 AppBarLayout 和 tabLayout 分开。这样您就可以独立修改标签栏和查看分页器属性。
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<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"
android:layout_above="@+id/tabs"/>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
android:layout_alignParentBottom="true"
在
中添加此设置android.support.design.widget.TabLayout
页面顶部LinearLayout设置和设置android:gravity="bottom"
。而已。这是代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px"
android:gravity="bottom"> //Thats it.
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:id="@+id/tabLayout1">
<android.support.design.widget.TabItem
android:icon="@drawable/home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabItem1" />
<android.support.design.widget.TabItem
android:icon="@drawable/mypage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabItem2" />
<android.support.design.widget.TabItem
android:icon="@drawable/friends"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabItem3" />
<android.support.design.widget.TabItem
android:icon="@drawable/messages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabItem4" />
<android.support.design.widget.TabItem
android:icon="@drawable/settings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabItem5" />
</android.support.design.widget.TabLayout>
</LinearLayout>
我建议使用 androidx 库,因为 android 支持库可能很快就会被弃用。
我们实际上可以向 viewpager 添加选项卡布局。下面是我在项目中使用的代码片段
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00a294"
android:layout_gravity="bottom" />
</androidx.viewpager.widget.ViewPager>
layout_gravity="bottom"是把tab布局放到最下面的语法