首页 > 解决方案 > 框架布局在 ConstraintLayout 内未正确定位

问题描述

我已经放置了一个FrameLayout内部ConstraintLayout,我认为它受到了适当的限制。但是在应用程序中,它没有正确定位。FrameLayout在运行时被适当的片段替换。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.chapters.ChapterActivity">


    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar_chapter"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:title="Chapters"
        app:titleTextColor="@color/White" />

    <FrameLayout
        android:id="@+id/chapter_fragment_container_frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:minHeight="500dp"
        app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.388"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar_chapter"
        app:layout_constraintVertical_bias="1.0" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@color/colorPrimary"
        app:itemIconTint="@color/White"
        app:itemTextColor="@color/Wheat"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/chapter_bottom_menu">

    </com.google.android.material.bottomnavigation.BottomNavigationView>


</androidx.constraintlayout.widget.ConstraintLayout>

以下是 IDE 中显示的内容:

IDE 设计预览

但这是我在设备中得到的:

应用程序在设备预览中运行

FrameLayout应该放在工具栏和底部导航栏之间,应该由LinearLayout/Relative一个完成,虽然没有尝试过..但这有什么问题。

标签: androidandroid-fragmentsandroid-constraintlayoutandroid-framelayout

解决方案


更改此 FrameLayout 部分,如下所示:

 <FrameLayout
        android:id="@+id/chapter_fragment_container_frameLayout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:minHeight="500dp"
        app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.388"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar_chapter"
        app:layout_constraintVertical_bias="1.0" />

它发生的原因是因为match_constraint。如果你已经定义了约束,top, bottom, start, and end你应该定义0dpforheightwidthto View。


推荐阅读