如何将Toolbar,FrameLayout,BottomNavigationView放在一个活动中?

时间:2018-02-02 17:48:54

标签: java android user-interface android-xml

当我将所有内容放在main.xml中时,这很有用,但是当我将组件移动到单独的xml并使用include时,bottomnavigationview不再位于底部:

   Map<String,Set<String>> map =  list.stream().
           collect(Collectors.groupingBy(
                    Student::getName,
                    Collectors.mapping(e->getNum(e.getAddr()), Collectors.toSet())
                ));

   System.out.println("Map : "+map);

这是我的toolbar.xml布局:

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <include
        android:id="@+id/activity_user_details_toolbar"
        app:layout_constraintBottom_toTopOf="@+id/activity_user_details_fragment_container"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        layout="@layout/toolbar" />

    <FrameLayout
        android:id="@+id/activity_user_details_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toTopOf="@+id/activity_user_details_bottom_navigation"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/activity_user_details_toolbar" />

    <include
        android:id="@+id/activity_user_details_bottom_navigation"
        layout="@layout/bottom_navigation"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>

首先,BottomNavigationView与工具栏重叠,因为无法识别FrameLayout约束。其次我无法访问工具栏,因为当我使用include时,它包括整个AppBarLayout而不仅仅是工具栏

2 个答案:

答案 0 :(得分:0)

include标记上调整布局参数时,您还需要指定layout_widthlayout_height

https://developer.android.com/training/improving-layouts/reusing-layouts.html

结果:

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <include
        android:id="@+id/activity_user_details_toolbar"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        layout="@layout/toolbar" />

    <FrameLayout
        android:id="@+id/activity_user_details_fragment_container"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/activity_user_details_bottom_navigation"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/activity_user_details_toolbar" />

    <include
        android:id="@+id/activity_user_details_bottom_navigation"
        layout="@layout/bottom_navigation"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>

答案 1 :(得分:-1)

你可以尝试一下,它会给你你想要的相同结果:

<android.support.constraint.ConstraintLayout 
     android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <include
        android:id="@+id/activity_user_details_toolbar"
        app:layout_constraintBottom_toTopOf="@+id/activity_user_details_fragment_container"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        layout="@layout/toolbar" />

    <FrameLayout
        android:id="@+id/activity_user_details_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toTopOf="@+id/activity_user_details_bottom_navigation"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/activity_user_details_toolbar" />

    <include
        android:id="@+id/activity_user_details_bottom_navigation"
        layout="@layout/bottom_navigation"
        android:layout_alignParentBottom="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_height="56dp"
        android:layout_width="match_parent"
        />

       </android.support.constraint.ConstraintLayout>