【问题标题】:Is it possible to to define all my layout only with constraints?是否可以仅使用约束来定义我的所有布局?
【发布时间】:2020-04-17 19:59:35
【问题描述】:

我正在尝试将 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

【问题讨论】:

    标签: android android-layout kotlin android-constraintlayout


    【解决方案1】:

    您可以使用layout_constraintVertical_weightlayout_constraintHorizontal_weight

    <?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="match_parent">
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/constraintLayout1"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#E91E63"
            app:layout_constraintBottom_toTopOf="@id/constraintLayout2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_weight="0.3">
    
        </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_weight="0.7">
    
            <Button
                android:id="@+id/button1"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:text="Button"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@id/button2"
                app:layout_constraintHorizontal_weight="0.5"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="@id/constraintLayout2" />
    
            <Button
                android:id="@+id/button2"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintHorizontal_weight="0.5"
                android:text="Button"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@id/button1"
                app:layout_constraintTop_toTopOf="@id/constraintLayout2" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

      【解决方案2】:

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

      您基本上会说您在宽度/高度的 0-100%(在您的情况下为 30% 或 50%)处创建一条水平/垂直线,然后将您的按钮与该指南连接

      另外,在不需要时在彼此内部拥有多个约束布局也是不好的做法,你应该只有一个

      如果您还有其他问题,请询问

      【讨论】:

      • 完美。谢谢。
      猜你喜欢
      • 2018-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-04
      • 2021-04-18
      • 1970-01-01
      • 2017-02-27
      • 2021-05-02
      相关资源
      最近更新 更多