android-fragments 使用导航抽屉的抽屉布局,如何设置片段容器(底部导航)以匹配父高度

0md85ypi  于 2022-11-14  发布在  Android
关注(0)|答案(1)|浏览(179)

为了添加一个汉堡菜单,我添加了drawer layout作为主相对布局的第一个子布局。在drawerlayout中,我添加了工具栏和其他视图。
现在,在主Activity中还有两个视图,其中一个是片段容器。
现在,如果我向fragment_container中添加一个layout_bellow="@+id/toolBar”,它会显示以下错误-toolBar is not a sibling in the same RelativeLayout
当我添加layout_below”@id/drawer_layout时,片段容器将位于整个抽屉的下方,即使抽屉处于关闭状态
我正在使用这个video应用汉堡菜单
下面是xml代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:fitsSystemWindows="true"
        tools:context=".MainActivity"
        tools:openDrawer="start">

        <LinearLayout
            android:id="@+id/toolbarLinearLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <com.google.android.material.appbar.MaterialToolbar
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                tools:ignore="ExtraText"
                android:id="@+id/toolBar"
                android:background="@color/feat_grey"
                android:elevation="4dp"
                app:contentInsetStart="0dp"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                tools:targetApi="lollipop">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:adjustViewBounds="true"
                    android:contentDescription="@string/todo"
                    android:elevation="4dp"
                    android:scaleType="centerInside"
                    android:src="@drawable/ic_featsystems_logo"
                    tools:targetApi="lollipop" />
            </com.google.android.material.appbar.MaterialToolbar>

        </LinearLayout>

        <com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_viewer"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/nav_header"
            app:menu="@menu/drawer_menue" />



    </androidx.drawerlayout.widget.DrawerLayout>

    <include
        android:id="@+id/fragment_container"
        layout="@layout/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottomNavigationView_border"
        android:layout_below="@+id/toolBar" />

    <View
        android:id="@+id/bottomNavigationView_border"
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:layout_above="@id/bottomNavigationView"
        android:background="@drawable/shadow"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/feat_grey"
        app:labelVisibilityMode="labeled"
        app:itemIconSize="30dp"
        app:itemTextColor="@drawable/bottom_nav_color_label"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/fragment_container"
        app:menu="@menu/bottom_nav_menu" />
</RelativeLayout>
aiqt4smr

aiqt4smr1#

使用此xml结构

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimaryDark">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/tbMain"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:background="@color/transparent"
            android:minHeight="?attr/actionBarSize"
            app:contentInsetStart="0dp"
            local:layout_constraintEnd_toEndOf="parent"
            local:layout_constraintStart_toStartOf="parent"
            local:layout_constraintTop_toTopOf="parent">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical">

                <androidx.appcompat.widget.AppCompatTextView
                    android:id="@+id/tvToolbarTitle"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:layout_alignParentEnd="true"
                    android:layout_centerVertical="true"
                    android:layout_toEndOf="@+id/ivOpenCloseDrawer"
                    android:fontFamily="@font/medium"
                    android:gravity="center_vertical"
                    android:maxLines="1"
                    android:text="@string/app_name"
                    android:textColor="@color/textColor"
                    android:textSize="@dimen/normalSize" />

                <androidx.appcompat.widget.AppCompatImageView
                    android:id="@+id/ivOpenCloseDrawer"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:layout_centerVertical="true"
                    android:padding="@dimen/smallPadding"
                    android:tint="@color/textColor"
                    android:visibility="visible"
                    local:srcCompat="@drawable/ic_ads_free" />
            </RelativeLayout>

        </androidx.appcompat.widget.Toolbar>

        <ScrollView
            android:id="@+id/svMain"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/bottomNavigationView_border"
            android:layout_below="@id/tbMain"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tbMain">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

    <!--                 Do your design in this-->

            </androidx.constraintlayout.widget.ConstraintLayout>

        </ScrollView>

        <View
            android:id="@+id/bottomNavigationView_border"
            android:layout_width="match_parent"
            android:layout_height="4dp"
            android:layout_above="@id/bottomNavigationView"
            android:background="@color/gray_light"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigationView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@color/gray"
            app:itemIconSize="30dp"
            app:itemTextColor="@color/textColor"
            app:labelVisibilityMode="labeled"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
              />

    </RelativeLayout>

<com.google.android.material.navigation.NavigationView
    android:id="@+id/navigationView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/colorPrimary"
    android:clickable="true"
    android:fitsSystemWindows="true"
    android:focusable="true"
    app:headerLayout="@layout/nav_header_main"
    app:itemIconTint="@color/textColor"
    app:itemTextColor="@color/textColor"
    app:menu="@menu/navigation_menu" />
</androidx.drawerlayout.widget.DrawerLayout>

相关问题