【问题标题】:Navigation Drawer Fragment Affecting Toolbar's Style and Positioning影响工具栏样式和定位的导航抽屉片段
【发布时间】:2018-08-26 03:49:44
【问题描述】:

我正在拼命寻求解决我遇到的问题的帮助。所以我一直在关注this 教程系列,这绝对是很棒的,但与使用视频中的Bottom Navigation 相比,我使用的是带有片段的“导航抽屉”。

当我在NavigationDrawer 中选择HomeFragment 时,布局会加载,但所有内容都被推到Toolbar 下方。现在,工具栏功能齐全,因为汉堡包和更多选项菜单图标确实在那里,但也是白色的,因此它更多的是设计问题。

另外,关于Toolbar's 样式,是的,我故意将其设置为透明,以便完成的 UI 看起来像下面的“预期输出”图像。

预期输出 - 这是HomeActivity 中与HomeFragment 相对的相同布局。

以下是我的HomeActivity 代码,它嵌套了FragmentsFrameLayout

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.example.michaeldybek.plasticoceansapp.HomeActivity">

    <FrameLayout
        android:id="@+id/mainContainer"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@id/toolbar"
        app:layout_constraintTop_toBottomOf="@id/toolbar">
    </FrameLayout>

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@null"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

我已经像正常一样在HomeActivity 类中声明并启动了Toolbar,当显示HomeActivity 时确实存在Toolbar,这让我怀疑是否必须在@ 中再次声明它987654341@上课?如果是这样,你会怎么做?

如果其他布局或类需要任何其他代码,请提出。任何帮助将不胜感激!

【问题讨论】:

    标签: android-layout android-studio android-fragments navigation-drawer android-toolbar


    【解决方案1】:

    在工具栏中删除 android:background="@null" 或设置 android:background="@color/colorPrimary"

    【讨论】:

    • 我试过了,可惜没用。正如我在帖子中所说,我希望Toolbar 是透明的,将background 设置为colorPrimary 将为它设置颜色。
    • 然后将您的背景颜色设置为父 ConstraintLayout
    • 对不起,这也不起作用。您不能将颜色设置为位置,它们是两个不同的东西。
    【解决方案2】:

    所以经过一周的扎实研究,我终于想出了一个绕过这个问题的解决方案!

    我最终从HomeActivity 中删除了ToolbarFrameLayout,而是创建了一个新的布局资源文件并将其命名为“app_bar.xml”,其中包含以下代码:

    <RelativeLayout
        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.support.design.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".core.MainActivity">
    
            <FrameLayout
                android:id="@+id/mainContainer"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
    
        </android.support.design.widget.CoordinatorLayout>
    
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:elevation="0dp"
            android:stateListAnimator="@null">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:theme="@style/ThemeOverlay.AppCompat.Light"/>
    
        </android.support.design.widget.AppBarLayout>
    
    </RelativeLayout>
    

    然后,我使用include 标记将app_bar 包含在HomeActivity 布局中(HomeActivity 还包含嵌套在DrawerLayout 中的NavigationView)。

    我希望这对遇到这个问题的人有所帮助。如果您还有任何疑问,请随时询问!

    【讨论】:

      猜你喜欢
      • 2016-05-03
      • 1970-01-01
      • 2014-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-15
      • 1970-01-01
      相关资源
      最近更新 更多