android - 基本约束布局(工具栏、 fragment 容器和底部导航)

标签 android android-layout android-constraintlayout

我正在适应适用于 Android 的 Kotlin 和 ConstraintLayouts。我正在尝试一个简单的布局,其中包含一个工具栏和一个底部导航 View 。

这是我当前的 xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimaryDark"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:textColor="@android:color/white"
            android:textSize="@dimen/toolbar_title_text_size"
            android:textStyle="bold" />

    </android.support.v7.widget.Toolbar>

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/toolbar"
        app:layout_constraintBottom_toTopOf="@id/bottom_navigation_view"/>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:menu="@menu/menu_main" />

</android.support.constraint.ConstraintLayout>

不幸的是,我误解了一些东西,似乎无法让 fragment 容器填充工具栏和底部导航之间的空间。

有人可以引导我朝着正确的方向前进吗?

最佳答案

fragment 容器中的高度需要为 0dp,相当于“MATCH_CONSTRAINT”。

android:layout_height="0dp"

您可以在这里查看ConstraintLayout .

应该是

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@color/colorAccent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@id/toolbar"
    app:layout_constraintBottom_toTopOf="@id/bottom_navigation_view"/>

关于android - 基本约束布局(工具栏、 fragment 容器和底部导航),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48045196/

相关文章:

android - 如何构建共享库并在其他ndk程序中调用

java - 如何从ListView项目中获取TextView?

android - 如何禁用 TextInputLayout 上的填充?

android linearlayout 和 textview wrap_content 导致剪切最后一行文本

java - GridView不显示任何元素

android - textwatcher 上的 IndexOutOfBoundsException

android - 如何为自定义控件创建事件

android - 关于 DrawerLayout 的错误(IllegalArgumentException : View android. widget.RelativeLayout is not a sliding drawer)

android - 无法将 FrameLayout 放置在 ConstraintLayout 内

android - 学习约束布局有问题