与ConstraintLayout问题 - 垂直边距不工作

与ConstraintLayout问题 - 垂直边距不工作

问题描述:

我想建立使用ConstraintLayout这个简单的布局:与ConstraintLayout问题 - 垂直边距不工作

Layout with two text views

它将按预期工作时标题微妙都只是单行文本。问题伴随着更长的文字。正如你所看到的,标题微妙相互重叠:

Results

下面是我使用的布局的源代码:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 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.ConstraintLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:background="#EEEEEE"> 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="24dp" 
      android:layout_marginLeft="16dp" 
      android:layout_marginRight="16dp" 
      android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." 
      android:textAppearance="@style/TextAppearance.AppCompat.Headline" 
      app:layout_constraintTop_toTopOf="parent" 
      app:layout_constraintLeft_toLeftOf="parent" 
      app:layout_constraintRight_toRightOf="parent" /> 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="16dp" 
      android:layout_marginLeft="16dp" 
      android:layout_marginRight="16dp" 
      android:layout_marginBottom="24dp" 
      android:text="Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." 
      app:layout_constraintTop_toBottomOf="@+id/textView1" 
      app:layout_constraintLeft_toLeftOf="parent" 
      app:layout_constraintRight_toRightOf="parent" 
      app:layout_constraintBottom_toBottomOf="parent" /> 

    </android.support.constraint.ConstraintLayout> 

</FrameLayout> 

那么,是什么问题?


EDIT 1 2017年10月5日下午1时19分

好像只能在运行Android 6.0(API 23)的设备/重放模拟器。在运行API 21-22,24+的设备上按预期工作。

我建议你只使用它给你chaining option的ConstraintLayout。有了它,你可以 2 TextViews和ConstraintLayout本身垂直居中他们没有FrameLayout里,你所面临的问题:ConstraintLayout版本1.0.0 beta5的,它看起来像

<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:background="#EEEEEE"> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="16dp" 
     android:layout_marginRight="16dp" 
     android:layout_marginTop="24dp" 
     android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." 
     android:textAppearance="@style/TextAppearance.AppCompat.Headline" 
     app:layout_constraintBottom_toTopOf="@+id/textView2" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintVertical_chainStyle="packed" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="24dp" 
     android:layout_marginLeft="16dp" 
     android:layout_marginRight="16dp" 
     android:layout_marginTop="16dp" 
     android:text="Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/textView1" /> 

</android.support.constraint.ConstraintLayout> 
+0

是的,“包装”链式解决了这个问题。谢谢! –

您的TextView1没有较低的边界。

只需添加

app:layout_constraintBottom_toTopOf="@+id/textView2" 

你的第一个TextView的

+0

您的解决方案的工作原理,但它只适用于这种特殊情况,如果我在这些文本视图之上添加ImageView,我会在布局中遇到其他奇怪的问题。看看这个问题,例如https://stackoverflow.com/questions/46588508/problems-with-constraintlayout-imageview-169-inappropriate-top-margin –

在API 23顶部边距不被尊重或顶部TextView的高度计算不正确。我确实在API 23上看到重叠,但在版本为ConstraintLayout的API 24上看不到。

但是,使用ConstraintLayout版本1.1.0-beta2,在API 23和API 24上的一切看起来都不错,所以这可能是一个已经被纠正的问题。我建议您升级到更高版本的ConstraintLayout以查看问题是否仍然存在。

+0

我已经用1.1.0-beta2测试过了,是的,看起来像是在该版本中修复的。但由于它仍处于测试阶段,所以我宁愿不使用它。看起来生产目前使用chain =“packed”更好。 –