首页 > 解决方案 > 为什么单个片段在一个活动中占据整个屏幕,而其他视图使用约束布局?

问题描述

我的主要活动中有两个框架布局,在运行时将被两个片段替换。我正在使用约束布局来对齐它们。一个特定的 Fragment 占据了 id为 recentsfragment 的framelayout正在替换整个屏幕,并且没有给其他 framelayout 任何位置。

当我单独显示带有片段的框架布局时,视图完全符合预期。

activity_main.xml

<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
>
 <androidx.constraintlayout.widget.ConstraintLayout


  android:layout_width="match_parent"
  android:layout_height="match_parent">

<FrameLayout
    android:id="@+id/recentsfragment"
    android:layout_width="395dp"
    android:layout_height="325dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.618"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/view"></FrameLayout>

<view
    android:id="@+id/view"
    class="android.widget.Button"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_marginBottom="8dp"
    android:background="@android:color/darker_gray"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:layout_editor_absoluteX="-51dp" />

<FrameLayout
    android:id="@+id/allnotesfragment"
    android:layout_width="395dp"
    android:layout_height="327dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toTopOf="@+id/view"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"></FrameLayout>

我为他们尝试了不同的对齐方式,但没有运气。我特别想使用约束布局。任何帮助表示赞赏。谢谢。

标签: androidandroid-layoutandroid-fragmentsandroid-constraintlayout

解决方案


你只需要改变你的两个 FrameLayout0dp就像android:layout_height="0dp".


推荐阅读