【问题标题】:How to set android TabLayout in the bottom of the screen?如何在屏幕底部设置android TabLayout?
【发布时间】:2016-01-27 14:43:35
【问题描述】:

我的问题是如何将新的 android 材料设计 TabLayout 设置在屏幕底部,有点像 Instagram 的底部工具栏。

如果你从未见过 Instagram 的用户界面,这里是它的屏幕截图:

。如果有更好的解决方法,请随时在此处发布(如果可能,请提供代码示例),我将不胜感激。

这是我的代码: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 社区建议的许多方法和变通方法,但似乎没有一个适用于 android 中这种新的选项卡实现。我知道这个 UI 设计不遵循 android 设计指南,所以请不要评论它。这个 UI 设计对我的应用程序的 UX 至关重要,如果能得到答案,我将不胜感激。谢谢!

【问题讨论】:

  • 完整解决方案在这里:stackoverflow.com/a/32985326/4409409
  • 请检查我的解决方案。如果我已经解决了您的问题,请务必将其标记为答案。
  • 非常感谢你们。你们两个都救了我的命。你们不知道我是多么感谢你们的帮助和我收到的宝贵答案。最好的问候!
  • @user562 非常欢迎您。编码愉快!
  • @user562 如果可能,请尽量避免底部的标签。 Instagram 从第一天开始就遵循 iOS 之类的标签。 Android 和 Material design 不建议这样做。

标签: android android-layout android-tablayout


【解决方案1】:

我相信我有最好的简单解决方法。使用 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>

作为奖励,您应该只创建一次工具栏作为工具栏.xml。因此,您所要做的就是使用包含标签。使您的布局更干净。享受吧!

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

【讨论】:

  • 如何在 xamarin 中制作?
  • 很适合我。我可以放更多图标然后底部导航栏
  • 很好的答案,谢谢!我还使用 TabLayout 上的 app:tabIndicatorGravity="top" 将选项卡指示器移到了顶部
【解决方案2】:

用这个替换你的 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" />

【讨论】:

    【解决方案3】:

    我在 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>
    

    【讨论】:

      【解决方案4】:

      像下面的代码一样,更好地隔离 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>
      

      【讨论】:

        【解决方案5】:
        android:layout_alignParentBottom="true"
        

        添加此设置

        android.support.design.widget.TabLayout
        

        【讨论】:

        • 没用,认为与更复杂的答案相比,这太好了,令人难以置信。
        【解决方案6】:

        页首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>
        

        【讨论】:

          【解决方案7】:

          我建议使用 androidx 库,因为 android 支持库可能很快就会被弃用。

          我们实际上可以将选项卡布局添加到 viewpager。下面是我在项目中使用的代码sn-p

          <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" 是将选项卡布局置于底部的语法

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-06-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多