BottomNavigationView布局问题

时间:2019-04-05 09:38:24

标签: android bottomnavigationview

我将BottomNavigationViewConstraintLayout一起使用。现在的问题是,当我单击第二个项目时,所有视图都将其隐藏起来。

点击前

enter image description here

点击后

enter image description here

<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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="56dp"
        android:text="@string/title_home"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <LinearLayout
        android:id="@+id/linearLayout4"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:gravity="bottom"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">
    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/navigation" />
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_home"
        android:icon="@mipmap/home_icon"
        android:title="@string/title_home" />

    <item
        android:id="@+id/navigation_dashboard"
        android:icon="@mipmap/my_order"
        android:title="My Order" />

    <item
        android:id="@+id/navigation_notifications"
        android:icon="@mipmap/home_earning"
        android:title="Earnings" />
    <item
        android:id="@+id/navigation_dashboard1"
        android:icon="@mipmap/cart_icon"
        android:title="Cart" />

    <item
        android:id="@+id/navigation_notifications2"
        android:icon="@mipmap/account_icon"
        android:title="Account" />


</menu>

3 个答案:

答案 0 :(得分:1)

不需要为此拥有LinearLayout。使用app:layout_constraintBottom_toBottomOf="parent"navigation view移到底部。

<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:id="@+id/container"
                                             android:layout_width="match_parent"
                                             android:layout_height="match_parent">


    <android.support.design.widget.BottomNavigationView
            app:layout_constraintBottom_toBottomOf="parent"
            app:labelVisibilityMode="labeled"
            android:id="@+id/navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            app:menu="@menu/navigation_contractor" />

    <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="@string/title_home"
            app:layout_constraintBottom_toTopOf="@+id/navigation"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintVertical_bias="0.0"/>


</android.support.constraint.ConstraintLayout>

答案 1 :(得分:1)

我对ConstraintLayout有类似的问题。你可以试试看。

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/container">

    <android.support.design.widget.BottomNavigationView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_gravity="bottom"
        android:background="@android:color/white"
        app:elevation="8dp"
        app:menu="@menu/navigation"
        android:layout_alignParentBottom="true">
    </android.support.design.widget.BottomNavigationView>

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        android:layout_above="@+id/bottom_navigation" />
</RelativeLayout>

答案 2 :(得分:1)

我发现了这个问题,已将其放置在我的家庭片段android:fitsSystemWindows="true"中,我将其删除了,并且效果很好。