如何在屏幕底部设置android TabLayout?

前端之家收集整理的这篇文章主要介绍了如何在屏幕底部设置android TabLayout?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我第一次在堆叠,所以请裸露我.我的问题是如何将新的 Android材质设计TabLayout设置在屏幕的底部,就像Instagram的底部工具栏.
If you have never seen Instagram’s UI here is a screenshot of it.如果有一个更好的方法来接近这个,请随时发布在这里(如果可能的话,代码示例),我会非常感激.

这是我的代码: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具有垂直方向,并且查看器位于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" />

更新11.2.2016:对于那些谁不知道如何膨胀的工具栏,这里是如何.确保您的活动扩展了AppCompatActivity,以便您可以调用setSupportActionBar()和getSupportActionBar().

Toolbar mToolbar = (Toolbar) findViewById(R.id.main_toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
原文链接:https://www.f2er.com/android/311807.html

猜你在找的Android相关文章