如何将Android AppBarLayout,Toolbar和TabLayout与片段一起使用 [英] How to use Android AppBarLayout, Toolbar and TabLayout with fragments

查看:104
本文介绍了如何将Android AppBarLayout,Toolbar和TabLayout与片段一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用NavigationDrawer和带有工具栏的AppBarLayout进行活动.不幸的是,当我在子片段中使用TabLayout时,在工具栏和TabLayout之间看到阴影.是否可能仅在TabLayout下面显示阴影?我不想将TabLayout移到我的活动中,因为我仅在一个片段中使用它.

I have activity with NavigationDrawer and AppBarLayout with Toolbar. Unfortunately when I am using TabLayout in child fragment I see shadow between toolbar and TabLayout. Is it possible display shadow below TabLayout only? I do not want to move TabLayout to my activity because I'm using it only in one fragment.

我可以看到一些解决此问题的方法:

I can see few solutions for this problem:

  • 在工具栏上禁用高程&TabLaout(不喜欢)
  • 从活动中删除工具栏并将其移动到片段中

您对在我的场景中如何正确使用它有任何想法吗?

Do you have any ideas how to use it properly in my scenario?

推荐答案

我遇到了同样的问题,很容易解决.只需从活动中删除AppBarLayout并将其放入片段中即可.这样,您可以将工具栏保留在活动中,将TabLayout保留在片段中.

I had the same issue and it is easy to fix. Just remove the AppBarLayout from the activity and put it in the fragment. By doing that you keep the Toolbar in the activity and the TabLayout with the shadow in the fragment.

活动:

<?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:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <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/AppTheme.PopupOverlay" />


    <FrameLayout
        android:id="@+id/main_activity_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>


</LinearLayout>

片段:

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">


    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v4.view.ViewPager>


</LinearLayout>

希望有帮助!

这篇关于如何将Android AppBarLayout,Toolbar和TabLayout与片段一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆