【问题标题】:Unexpected results for android xml layoutandroid xml布局的意外结果
【发布时间】:2021-12-03 19:24:50
【问题描述】:

我是 android 开发的初学者,经常收到奇怪的结果。下面是我希望看到红色和绿色连续矩形的标记。但实际上我只看到绿色区域:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="200dp"
    android:layout_height="100dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/first_constrain"
        android:layout_width="100dp"
        android:layout_height="match_parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:background="#FF0000" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/second_constrain"
        android:layout_width="100dp"
        android:layout_height="match_parent"
        app:layout_constraintStart_toEndOf="@+id/first_constrain"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:background="#00FF00">

    </androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>

请解释一下我的代码有什么问题。

【问题讨论】:

  • FrameLayout 将您的元素一个接一个地排列,因此您可以看到屏幕上的最后一个元素。简单的解决方案,只需将 FrameLayout 替换为 LinearLayout

标签: java android android-layout android-fragments


【解决方案1】:

这是因为父 FrameLayout - FrameLayout 将元素(子)排列在彼此之上。

您需要将父布局更改为其他布局,例如LinearConstraintLayout 布局 - 类似这样。

<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="200dp"
    android:layout_height="100dp"
    tools:context=".MainActivity">
    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/first_constrain"
        android:layout_width="100dp"
        android:layout_height="match_parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:background="#FF0000" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/second_constrain"
        android:layout_width="100dp"
        android:layout_height="match_parent"
        app:layout_constraintStart_toEndOf="@+id/first_constrain"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:background="#00FF00"/>


</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

  • 我需要使用 FrameLayout。我预计 app:layout_constraint... 可以保证红色和 grren 部分的位置。
  • 文档说 FrameLayout 的设计目的是在屏幕上屏蔽一个区域以显示单个项目。一般来说,FrameLayout应该用于容纳单个子视图。你可以read more about here
猜你喜欢
  • 1970-01-01
  • 2021-10-27
  • 1970-01-01
  • 1970-01-01
  • 2012-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多