【问题标题】:Pack two adjacent views in a spread-chain ConstraintLayout在传播链 ConstraintLayout 中打包两个相邻的视图
【发布时间】:2017-03-29 07:10:26
【问题描述】:

我有一组六个小部件,它们在 ConstraintLayout 的展开链中链接在一起。此视图中没有其他布局,只有 ConstraintLayout 和小部件。

中间的两个小部件应该靠得更近,因为一个是另一个的标题:

widget-title
widget-EditText

我想在我的视图中的所有小部件上使用扩展链(垂直居中),但我希望这两个特别是垂直打包在一起,同时尊重顶部的底部边距和顶部的边距底部的。

我不知道该怎么做。我尝试将它们作为垂直 LinearLayout 的子级放在一起,但它拒绝正确参与传播链 - 它进入顶部并且许多其他小部件消失了。

我尝试向这两个视图添加额外的约束,但扩展链似乎覆盖了所有额外的约束并且它们没有效果。唯一的例外是对齐它们的基线,这确实有效,但会将它们混合在一起,这是我不想要的。

我没有看到一种方法可以在同一个 ConstraintLayout 中创建多个传播链,以使它们都均匀传播。

您如何将两个(或更多)小部件分组到一个链中,以使它们的传播规则与链的其余部分不同?或者如果做不到这一点,你如何构建多个分布相同的链?

【问题讨论】:

  • 一张图片在这里会有所帮助。您是否考虑过在ConstraintLayout 中使用指南?这可能会给您一些额外的灵活性。

标签: android-layout android-constraintlayout


【解决方案1】:

当您将两个中心小部件映射到 LinearLayout 时,一定是出了点问题。以下布局就是这样做的,并且似乎按照我认为您想要的方式工作。我没有在链中添加很多Views,但足以得到这个想法。看看吧。

<?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/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView1"
        app:layout_constraintBottom_toTopOf="@id/layout"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="spread" />

    <LinearLayout
        android:id="@+id/layout"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toTopOf="@id/textView4"
        app:layout_constraintTop_toBottomOf="@id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:text="TextView2" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="TextView3" />
    </LinearLayout>

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView4"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/layout" />

</android.support.constraint.ConstraintLayout>

【讨论】:

  • 您的回答说服我回去再试一次。我能够以全新的布局重现 Android Studio 中的原始行为——LinearLayout 再次表现不佳。但是,这一次我尝试将其 layout_height 设置为 wrap_content,这解决了所有问题。所以谢谢你。
猜你喜欢
  • 2020-04-30
  • 1970-01-01
  • 2014-12-10
  • 2013-05-03
  • 1970-01-01
  • 2020-03-26
  • 2020-10-13
  • 2020-01-22
  • 1970-01-01
相关资源
最近更新 更多