【发布时间】: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