0

我试图在 ConstraintLayout 中复制这个简单的用例。基本上它是 2 个彼此对齐的视图,但在父级中居中。

在此处输入图像描述

如果没有 CL,它将如下所示

FrameLayout
 match_parent
 match_parent

 LinearLayout
   orientation vertical
   width wrap_content
   height wrap_content
   layout_gravity center_horizontal
   gravity left

   View
    width wrap_content
   View
    width wrap_content

由于 CL 我正在努力如何将它们作为一个组居中,我可以将它们分别居中,并限制左父、右父,但是我希望该组居中,并且与该组一起使用左重力。我尝试在两个视图的左侧和右侧使用障碍,但它不会水平居中链。我认为,基本上我需要一条跨越障碍的水平链。

编辑:

I----AAA  ----I
I----BBBBB----I
I-------------I
I-------------I
4

3 回答 3

0

也许下面的代码会解决你的问题

<TextView
    android:id="@+id/text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text2 Test2 Test2"
    app:layout_constraintTop_toBottomOf="@id/centered"
    app:layout_constraintStart_toStartOf="@id/text1" />

<TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text1"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toTopOf="@+id/centered"
    app:layout_constraintStart_toStartOf="parent" />

<android.support.constraint.Guideline
    android:id="@+id/centered"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.50" />

于 2018-02-05T06:33:08.350 回答
0

假设您有两个视图(view1 较短但位于顶部,view2 较长但位于底部)。

你可以这样做:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:apps="http://schemas.android.com/apk/res-auto">

    <View
        android:id="@+id/view1"
        android:layout_width="120dp"
        android:layout_height="30dp"
        android:background="@android:color/holo_blue_bright"
        apps:layout_constraintLeft_toLeftOf="@+id/view2"
        apps:layout_constraintTop_toTopOf="parent"
        apps:layout_constraintBottom_toTopOf="@+id/view2"
        apps:layout_constraintVertical_chainStyle="packed"
        />

    <View
        android:id="@+id/view2"
        android:layout_width="240dp"
        android:layout_height="30dp"
        android:background="@android:color/holo_blue_dark"
        apps:layout_constraintLeft_toLeftOf="parent"
        apps:layout_constraintRight_toRightOf="parent"
        apps:layout_constraintTop_toBottomOf="@+id/view1"
        apps:layout_constraintBottom_toBottomOf="parent"
        />

</android.support.constraint.ConstraintLayout>

请注意layout_constraintVertical_chainStyle设置为packed将两个视图推到一起使用。

此外,观察两个视图之间的约束,使其能够按照您想要的方式进行布局。View2 在某种程度上决定了 view1 如何居中。在这里使用的一个好处ConstraintLayout是您不需要定义一个额外的组 ( ViewGroup) 只是为了将两个视图集中在一起,只需定义足够好的约束,它就会发挥它的魔力。

这将产生如下内容: 在此处输入图像描述

我不确定这是否是您要找的东西?

于 2018-02-14T09:06:04.353 回答
0

您可以以编程方式执行此操作:

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

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="28dp"
        android:background="#303F9F"
        android:text="Short Text"
        app:layout_constraintEnd_toStartOf="@+id/guideline2"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="@+id/guideline2"
        app:layout_constraintTop_toTopOf="parent"
        android:gravity="start" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="24dp"
        android:background="#303F9F"
        android:text="This is a very long text"
        app:layout_constraintEnd_toStartOf="@+id/guideline2"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="@+id/guideline2"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        android:layout_marginRight="8dp"
        android:gravity="start" />

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

</android.support.constraint.ConstraintLayout>

背景颜色是看到变化。

在代码中获取较大 TextView 的宽度并将其设置为较小的。

textView1 = findViewById(R.id.textView);
textView2 = findViewById(R.id.textView2);
textView1.measure(0,0);
textView2.measure(0,0);
int len1 = textView1.getMeasuredWidth();
int len2 = textView2.getMeasuredWidth();
if (len1 > len2) {
    textView2.getLayoutParams().width = len1;
} else {
    textView1.getLayoutParams().width = len2;
}

而且您不必总是使用ConstraintLayout.

于 2018-02-03T09:37:00.263 回答