【问题标题】:How to add a percentage margin between two views in ConstrainLayout如何在 ConstraintLayout 中的两个视图之间添加百分比边距
【发布时间】:2020-07-18 02:01:51
【问题描述】:

在 ConstraintLayout 中,要指定百分比边距,您可以使用指南。例如,使用此指南,您可以指定 imageView 视图距顶部有 1% 的屏幕高度边距:

<android.support.constraint.Guideline
    android:id="@+id/guideline7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.1" />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:src="@drawable/gamelogo"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHeight_percent="0.14"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/guideline7"/>

它有效,但是......如果您需要指定两个视图之间的百分比边距会发生什么?我试图为指南设置约束,但它忽略了它们并设置在屏幕顶部。因此,似乎无法指定两个视图之间的百分比边距。

怎么做?

【问题讨论】:

    标签: android android-layout android-constraintlayout


    【解决方案1】:

    您可以在布局中使用&lt;Space&gt; 元素来实现此目的。假设您想要 10% 的左右边距和两个视图之间 20% 的间隙来填充其余空间:

    <?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.Guideline
            android:id="@+id/startMargin"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.1"/>
    
        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/endMargin"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.9"/>
    
        <View
            android:id="@+id/first"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#fac"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toEndOf="@id/startMargin"
            app:layout_constraintEnd_toStartOf="@id/space"
            app:layout_constraintBottom_toBottomOf="parent"/>
    
        <Space
            android:id="@+id/space"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toEndOf="@id/first"
            app:layout_constraintEnd_toStartOf="@id/second"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintWidth_percent="0.2"/>
    
        <View
            android:id="@+id/second"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#caf"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toEndOf="@id/space"
            app:layout_constraintEnd_toStartOf="@id/endMargin"
            app:layout_constraintBottom_toBottomOf="parent"/>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    此时,如果你想改变两个视图之间的差距,你所要做的就是改变app:layout_constraintWidth_percent属性。

    【讨论】:

      【解决方案2】:

      位于指南端点的 2 个视图应在 ConstraintLayout 之外。

      例如,组件树应该如下所示:

      LinearLayout
          ImageView
          ConstraintLayout
              Guideline (horizontal)
              ImageView ---> The one that makes use of the Guideline
          ImageView
      

      ConstraintLayout 应使用以下设置:

      <ConstraintLayout
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight="1"
          android:layout_gravity="fill_horizontal">
      

      【讨论】: