是否可以仅在约束条件下定义所有布局?

问题描述 投票:0回答:2

我正在尝试将constraintLayout1的高度定义为可用大小的30%。其余为constraintLayout2。但是我想定义百分比而不是像素。Button1应该是50%,而button2其他应该是50%。我想用约束而不是用固定值来定义所有内容(就像我现在所做的marginBottom="500dp)。这有可能吗?我正在使用Android 3.6.2。

<?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=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayout1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="500dp"
        android:background="#E91E63"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0">

    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayout2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#8BC34A"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/constraintLayout1"
        app:layout_constraintVertical_bias="0.0">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:text="Button"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:text="Button"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
```[enter image description here][1]


  [1]: https://i.stack.imgur.com/NzZwz.png

我正在尝试将constraintLayout1的高度定义为可用大小的30%。其余为constraintLayout2。但是我想定义百分比而不是像素。 Button1应该是50%...

android android-layout kotlin android-constraintlayout
2个回答
0
投票

您可以使用layout_constraintVertical_weightlayout_constraintHorizontal_weight


0
投票

是的,一种简单的方法是Guidelines

© www.soinside.com 2019 - 2024. All rights reserved.