0

我想建立使用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+的設備上按預期工作。

回答

1

我建議你只使用它給你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

是的,「包裝」鏈式解決了這個問題。謝謝! –

0

您的TextView1沒有較低的邊界。

只需添加

app:layout_constraintBottom_toTopOf="@+id/textView2" 

你的第一個TextView的

+0

您的解決方案的工作原理,但它只適用於這種特殊情況,如果我在這些文本視圖之上添加ImageView,我會在佈局中遇到其他奇怪的問題。看看這個問題,例如https://stackoverflow.com/questions/46588508/problems-with-constraintlayout-imageview-169-inappropriate-top-margin –

3

在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」更好。 –