简体   繁体   中英

Bottom Navigation Bar not shown in activity

I have an activity with a title, a fragment with a recyclerview full of cards and a bottom navigaton bar.

I cannot manage to show all 3 together without problems, the closest I have been is showing all 3, but with the bar taking space from the cards space.

Right now this is the XML which I thought should be working:

<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:orientation="vertical"
tools:context=".habits.HabitsActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="313dp"
        android:layout_height="166dp"
        android:layout_gravity="center_horizontal"
        app:srcCompat="@drawable/goodreasons" />


<android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

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


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="bottom">


<com.aurelhubert.ahbottomnavigation.AHBottomNavigation
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom" />

</FrameLayout>


</LinearLayout>

But this gives this output:

在此处输入图片说明

This is the xml of the fragment, in case you think it is relevant too.

<?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:layout_gravity="center"
    android:orientation="vertical"
    android:padding="5dp"

>
<android.support.v7.widget.RecyclerView
    android:id="@+id/rv_list"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0"
    android:layout_gravity="center"
    />


</LinearLayout>

Here's your layout, stripped down:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:layout_width="313dp"
        android:layout_height="166dp"/>

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

Your problem is that the CoordinatorLayout has a match_parent height, so it will take up all of the space that the ImageView doesn't. That leaves no space at all for the FrameLayout , so you won't be able to see anything inside of it.

Probably the best thing to do would be to use layout_weight on the CoordinatorLayout to make it take up as much space as it can while still leaving room for other views. That would look something like this:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:layout_width="313dp"
        android:layout_height="166dp"/>

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

The only changes I made were to set the CoordinatorLayout's height to 0dp and the FrameLayout's height to wrap_content , and to add the weight attribute to the CoordinatorLayout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM