我的问题是如何将新的 android Material 设计 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 至关重要,如果能得到答案,我将不胜感激。谢谢!
最佳答案
我相信我有最好的简单解决方法。使用 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);
关于android - 如何在屏幕底部设置android TabLayout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33380668/