如何使底部导航停留在LinearLayout的底部?

时间:2019-10-05 17:44:51

标签: android xml

如何使用LinearLayout使底部导航停留在页面底部?我发现的大多数解决方案是,他们使用RelativeLayout而不是LinearLayout。

下面是我的代码

<?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"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

    <androidx.appcompat.widget.Toolbar

        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/purpleBoo"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text=" MY ACCOUNT"
            android:textStyle="bold"
            android:textColor="@color/white">

        </TextView>
    </androidx.appcompat.widget.Toolbar>


    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/btm_nav"
        app:itemIconTint="@color/bottom_nav_color"
        app:itemTextColor="@color/bottom_nav_color"
        app:menu="@menu/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/purpleBoo"
        android:clipToPadding="false" />

</LinearLayout>


1 个答案:

答案 0 :(得分:0)

这很容易-您的xml应该如下所示:

<?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"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

    <androidx.appcompat.widget.Toolbar

        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/purpleBoo"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text=" MY ACCOUNT"
            android:textStyle="bold"
            android:textColor="@color/white">

        </TextView>
    </androidx.appcompat.widget.Toolbar>
    <!--Just add in between the action bar and bottom bar some other view with height = 0dp and weight = 1. I added one more LinearLayout-->
    <LinearLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical">

    </LinearLayout>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/btm_nav"
        app:itemIconTint="@color/bottom_nav_color"
        app:itemTextColor="@color/bottom_nav_color"
        app:menu="@menu/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/purpleBoo"
        android:clipToPadding="false" />

</LinearLayout>

在其他情况下,它将不起作用。为此,weight中至少应有一个基于height而不是基于LinearLayout的视图。

希望有帮助。