在约束布局中是否可以将4个TextView放在水平链中,每个视图之间的间距不同?

问题描述 投票:1回答:1

ConstraintLayout中,是否可以将4个TextView放置在水平链中,每个视图之间的间距不同?

例如,如下面的图片,我希望在不同的屏幕尺寸下,TextView的位置相同。

enter image description here

android android-constraintlayout
1个回答
0
投票

我可以想到一种不用链的替代解决方案。您可以使用Guideline获得您要设计的布局。我为您创建了一个。让我知道是否有帮助。

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.constraint.Guideline
        android:id="@+id/guideline_right_margin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent=".9" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline_left_margin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent=".1" />

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

    <TextView
        android:id="@+id/text_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text 1"
        android:textSize="20sp"
        app:layout_constraintStart_toStartOf="@+id/guideline_left_margin"
        app:layout_constraintTop_toBottomOf="@+id/guideline_top_margin" />

    <TextView
        android:id="@+id/text_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text 2"
        android:textSize="20sp"
        app:layout_constraintEnd_toStartOf="@+id/text_3"
        app:layout_constraintStart_toStartOf="@+id/text_1"
        app:layout_constraintTop_toBottomOf="@+id/guideline_top_margin" />

    <TextView
        android:id="@+id/text_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="16dp"
        android:text="Text 3"
        android:textSize="20sp"
        app:layout_constraintEnd_toStartOf="@+id/text_4"
        app:layout_constraintTop_toBottomOf="@+id/guideline_top_margin" />

    <TextView
        android:id="@+id/text_4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text 4"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="@+id/guideline_right_margin"
        app:layout_constraintTop_toBottomOf="@+id/guideline_top_margin" />

</android.support.constraint.ConstraintLayout>

这里是纵向模式下的外观。

portrait

这就是在横向模式下的外观。

landscape

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