2017-08-27 85 views
1
元素

我有以下布局:覆盖在ConstraintLayout

<?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" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#EEEEEE" 
    > 

    <ImageView 
     android:layout_width="4dp" 
     android:layout_height="match_parent" 
     android:background="@color/colorAccent" 
     /> 

    <TextView 
     android:text="12345678911131517192123252729313335373941434547495153555759616365676971737577798183858789" 
     android:id="@+id/title" 
     android:textSize="15sp" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     android:layout_marginRight="16dp" 
     android:layout_marginLeft="8dp" 
     android:layout_marginStart="8dp" 
     android:layout_marginEnd="16dp" 
     /> 

    <TextView 
     android:text=" text text" 
     android:id="@+id/prev" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     app:layout_constraintTop_toBottomOf="@+id/title" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     android:layout_marginLeft="8dp" 
     android:layout_marginStart="8dp" 
     /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="WHY WHY WHY" 
     app:layout_constraintTop_toBottomOf="@+id/prev" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     android:layout_marginBottom="8dp" 
     android:layout_marginLeft="8dp" 
     android:layout_marginStart="8dp" 
     /> 


</android.support.constraint.ConstraintLayout> 

我们有以下结果: image with this issue

此外,如果我们改变了第一的TextView到文本:“1234567891113151719212325272931333537394143454749515355575961636567697173757779818385878991” (也就是添加2个数字),最后一个TextView会停止关闭前一个。另外,如果我们在上一个TextView中将layout_marginBottom属性更改为0dp(并且文本将像在示例中一样离开),问题也会消失。这个问题的原因是什么?如何解决它?

UPDATE:

在左侧加入具有高度match_parent的路径。因为这个不能在ConstraintLayout中使用paddingBottom。该布局用于RecyclerView,这就是为什么底部元素需要layout_marginBottom。

回答

0

我想通过删除app:layout_constraintBottom_toBottomOf="parent"将解决您的问题

出现此问题,因为你已经给父母ConstraintLayout android:layout_height="wrap_content"因此,它会自动尝试指定边界

编辑自身内部安排:

我已尝试&测试下面的代码在我的RecyclerView工作,希望它也适用于你。

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

<!--Your Test ImageView--> 
    <!--<ImageView 
     android:layout_width="4dp" 
     android:layout_height="match_parent" 
     android:background="@color/colorAccent" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintTop_toTopOf="parent" />--> 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginEnd="16dp" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="16dp" 
     android:layout_marginStart="8dp" 
     android:text="12345678911131517192123252729313335373941434547495153555759616365676971737577798183858789" 
     android:textSize="15sp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" /> 

    <TextView 
     android:id="@+id/prev" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginEnd="16dp" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="16dp" 
     android:layout_marginStart="8dp" 
     android:text=" text text" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/title" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginEnd="16dp" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="16dp" 
     android:layout_marginStart="8dp" 
     android:paddingBottom="32dp" 
     android:text="WHY WHY WHY" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/prev" /> 


</android.support.constraint.ConstraintLayout> 

希望它有帮助!

+0

由于底部边距消失,它并未完全解决问题。 –

+0

如果它是您在xml中需要的最后一个小部件,那么您的'layout_marginBottom'将不起作用 –

+0

不,这只是一个示例,实际上这样的布局具有背景,并且在RecyclerView中使用。 –

0

错误发生,因为在过去的TextView该行app:layout_constraintBottom_toBottomOf="parent" ..因为ConstraintLayout像RelativeLayout的
仅仅指刚删除此行

1

无论您从textview3app:layout_constraintBottom_toBottomOf="parent"或更改布局的高度match_parent

希望这会有所帮助! :)

+0

我需要一个wrap_content。如果删除app:layout_constraintBottom_toBottomOf =“parent”,layout_marginBottom停止工作。 –

+0

你解决了一个很大的问题 –

0

从textview3中删除app:layout_constraintBottom_toBottomOf="parent"像其他人所建议的,并将android:paddingBottom="8dp"添加到父约束。您也可以在textview3中添加一个marginTop参数以将其与textview2隔开。

0

我有一个非常类似的问题,这个Problems with ConstraintLayout - vertical margin doesn't work和唯一的解决方案是使用“包装”垂直链。所有这些“......添加/删除layout_constraintBottom_toBottomOf =”父母“...”只是为特定情况解决问题的技巧。

这里是集成了“打包”垂直产业链布局:

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

    <ImageView 
     android:layout_width="4dp" 
     android:layout_height="0dp" 
     android:background="@color/colorAccent" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintBottom_toBottomOf="parent" /> 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="16dp" 
     android:textSize="15sp" 
     android:text="12345678911131517192123252729313335373941434547495153555759616365676971737577798183858789" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintBottom_toTopOf="@+id/prev" 
     app:layout_constraintVertical_chainStyle="packed" /> 

    <TextView 
     android:text=" text text" 
     android:id="@+id/prev" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="8dp" 
     app:layout_constraintTop_toBottomOf="@+id/title" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintBottom_toTopOf="@+id/textView2" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="8dp" 
     android:text="WHY WHY WHY" 
     app:layout_constraintTop_toBottomOf="@+id/prev" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintBottom_toBottomOf="parent" /> 

</android.support.constraint.ConstraintLayout> 

它可以正常工作的任何文本视图长度和ConstraintLayout妥善包装的内容,看看这个GIF:

Result view

您可以在这里找到使用ConstraintLayout构建的RecyclerView项目布局的更复杂的实现https://github.com/eugenebrusov/cards