【发布时间】:2020-01-22 02:06:50
【问题描述】:
在ConstraintLayout 中,我需要假设两个视图作为一个组,并将该组中心水平放置在父级中,如下图所示:
这是我的 xml 代码:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<TextView
android:id="@+id/view_a"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:background="@android:color/holo_orange_dark"
android:gravity="center"
android:text="View A"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toStartOf="@id/view_b"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/view_b"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="@android:color/holo_red_light"
android:gravity="center"
android:text="View B"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/view_a"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我已经看过This Answer,但是当两个视图具有相同的宽度时它可以工作。我的视图宽度不一样,所以Guideline 不起作用!
我该怎么做?
【问题讨论】:
-
你试过链接吗?您尝试了哪些选项?
-
@ShwetaChauhan 这个问题包含我的目标照片
-
@ShwetaChauhan 感谢您抽出宝贵时间。 Pawel 的回答很简单,而且很有效。
-
@ShwetaChauhan 我做了宽度并且反应非常正确
标签: android android-xml android-constraintlayout centering constraint-layout-chains