【问题标题】:Constraint Layout align two views vertically and horizontally约束布局垂直和水平对齐两个视图
【发布时间】:2019-08-31 20:38:54
【问题描述】:

我正在尝试将两个视图一个接一个地对齐,垂直和水平居中。 视图的大小动态设置为屏幕的大小,这是屏幕的 XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">


<ImageView
    android:id="@+id/yellow"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="#faff68"
    app:layout_constraintHeight_default="percent"
    app:layout_constraintHeight_percent="0.6"
    app:layout_constraintDimensionRatio="1:1"
    app:layout_constraintBottom_toBottomOf="@id/purple"
    app:layout_constraintLeft_toLeftOf="@id/purple"
    app:layout_constraintRight_toRightOf="@id/purple"
    app:layout_constraintTop_toTopOf="@id/purple" />

<ImageView
    android:id="@+id/purple"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="#c303fa"
    app:layout_constraintHeight_default="percent"
    app:layout_constraintHeight_percent="0.5"
    app:layout_constraintDimensionRatio="1:1"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

结果不如预期

如何使用约束布局将黄色图像置于紫色图像的中心? (我知道如何在其他布局中实现)

【问题讨论】:

  • 我已经更新了我的答案,以展示如何使用 Circular Positioning 来实现你想要的。请检查一下,干杯。

标签: android android-layout android-constraintlayout


【解决方案1】:

更新 -

您可以使用Circular Positioning 将较大的视图置于较小视图的中心。

<ImageView
    android:id="@+id/yellow"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="#faff68"
    app:layout_constraintHeight_default="percent"
    app:layout_constraintHeight_percent="0.5"
    app:layout_constraintDimensionRatio="1:1"
    app:layout_constraintBottom_toBottomOf="@id/purple"
    app:layout_constraintLeft_toLeftOf="@id/purple"
    app:layout_constraintRight_toRightOf="@id/purple"
    app:layout_constraintTop_toTopOf="@id/purple"
    app:layout_constraintCircle="@id/purple"
    app:layout_constraintCircleRadius="0dp"/>

<ImageView
    android:id="@+id/purple"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="#c303fa"
    app:layout_constraintHeight_default="percent"
    app:layout_constraintHeight_percent="0.4"
    app:layout_constraintDimensionRatio="1:1"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

您可以固定较大的视图并将较小的视图置于其前面,而不是尝试将较大的视图居中放在较小的视图后面。

以下几行应该可以工作,

<ImageView
    android:id="@+id/yellow"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="#faff68"
    app:layout_constraintHeight_default="percent"
    app:layout_constraintHeight_percent="0.5"
    app:layout_constraintDimensionRatio="1:1"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

<ImageView
    android:id="@+id/purple"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="#c303fa"
    app:layout_constraintHeight_default="percent"
    app:layout_constraintHeight_percent="0.4"
    app:layout_constraintDimensionRatio="1:1"
    app:layout_constraintBottom_toBottomOf="@id/yellow"
    app:layout_constraintLeft_toLeftOf="@id/yellow"
    app:layout_constraintRight_toRightOf="@id/yellow"
    app:layout_constraintTop_toTopOf="@id/yellow" />

【讨论】:

    【解决方案2】:

    试试这个方法

     <ImageView
        android:id="@+id/yellow"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#faff68"
        app:layout_constraintHeight_default="percent"
        app:layout_constraintHeight_percent="0.6"
        app:layout_constraintBottom_toBottomOf="@id/purple"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@id/purple" />
    
    <ImageView
        android:id="@+id/purple"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#c303fa"
        app:layout_constraintHeight_default="percent"
        app:layout_constraintHeight_percent="0.5"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    

    【讨论】:

    • 但在这种情况下,黄色图像与紫色图像的比例不同。
    【解决方案3】:

    如果您希望黄色与其他图像的大小相同,只需更改黄色图像中的这一行

    app:layout_constraintHeight_percent="0.6"
    

    至此:

    app:layout_constraintHeight_percent="0.5"
    

    您需要做的就是使app:layout_constraintHeight_percent 的值在两个图像上相同。

    在您的情况下,一张图片的屏幕尺寸为 50%,而另一张图片的屏幕尺寸为 60%,因此它们看起来尺寸不同

    【讨论】:

    • 不,我不希望它们的大小相同。黄色应该看起来像紫色的边界。
    • 所以也许只是告诉黄色图像比紫色图像大 1% 或 2%,这会让你的黄色看起来像边框。
    • 例如,紫色的使用app:layout_constraintHeight_percent="0.5",黄色的使用app:layout_constraintHeight_percent="0.52"(大2%)
    • 我就是这样做的,只是我设置为0.6为例。如果我将其设置为 0.52,则一般结果将是相同的。
    • 如果你唯一需要做的就是做一个黄色边框为什么不使用here提到的自定义背景,你真的需要额外的视图吗?
    【解决方案4】:

    用这个替换你的黄色图像

    <ImageView
        android:id="@+id/yellow"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#faff68"
        app:layout_constraintHeight_default="percent"
        app:layout_constraintHeight_percent="0.6"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
    

    【讨论】:

    • 我不想将它与父对齐,因为在其他场景中,图像可以放置在屏幕的许多其他部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 2019-10-30
    • 2013-04-25
    • 1970-01-01
    • 2012-10-10
    相关资源
    最近更新 更多