安卓工作室线性布局问题:没有嵌套权重,我应该如何布局我的?



image

对于我LinearLayout(水平(的树。它有一个图像和其他LinearLayout,其中有文本和按钮。

我试图为文本和按钮赋予权重,它让我使用嵌套权重。我应该如何修复它而不会出错?

如果你想在这里使用LinearLayout,没有嵌套权重是没有办法的。Google 建议使用 ConstraintLayout 来保持布局的较低层次结构。

<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/glCenterVertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5"/>
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="150dp"
app:layout_constraintEnd_toEndOf="@id/glCenterVertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<Space
android:id="@+id/space"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@id/imageView"
app:layout_constraintStart_toEndOf="@id/imageView"
app:layout_constraintTop_toTopOf="@id/imageView"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@id/space"
android:text="Test"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/imageView"
app:layout_constraintTop_toTopOf="@id/imageView"/>
<Button
android:layout_width="wrap_content"
app:layout_constraintBottom_toBottomOf="@id/imageView"
android:text="Test"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/imageView"
app:layout_constraintTop_toTopOf="@id/space"
android:layout_height="wrap_content"/></androidx.constraintlayout.widget.ConstraintLayout>

相关内容

最新更新