如何在ConstrainLayout中的两个视图之间添加百分比裕度

在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"/>

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

如何做到?

thebestme_1 回答:如何在ConstrainLayout中的两个视图之间添加百分比裕度

您可以在布局中使用<Space>元素来实现此目的。假设您要在左边/右边留出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>

enter image description here

在这一点上,如果要更改两个视图之间的间隔,只需更改app:layout_constraintWidth_percent属性。

,

位于指南端点的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">
本文链接:https://www.f2er.com/3164642.html

大家都在问