2017-08-10 166 views
-1

我试图设置自定义视图的高度,以便它等于它的父项。ConstraintLayout,layout_height中的0dp,但视图向左移动?

下面的布局并没有正确设置自定义视图的高度,以其父(我已记录到OnMeasure调用)。但是,限制布局无法正确定位视图。只要layout_height设置为0dp,视图就会向左移动(包括在android studio设计器中)。

<android.support.constraint.ConstraintLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    > 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Hello" 
     android:layout_marginTop="20dp" 
     android:layout_marginBottom="20dp" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintBottom_toBottomOf="parent" 
     /> 

    <View 
     android:background="#ff0000" 
     app:layout_constraintRight_toLeftOf="parent" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_width="50dp" 
     android:layout_height="10dp" 
     /> 
</android.support.constraint.ConstraintLayout> 

如果我设置此高度10dp出现图。

ContraintLayout似乎隐藏了视图,因为它的某个属性中有0dp。这怎么可能 ?

doc解释说应改用MATCH_CONSTRAINT。

我在做什么错了?

+3

你已将它设置为应用程序:layout_constraintRight_toLeftOf你可能表示应用程序:layout_constraintRight_toRightOf – MidasLefko

+0

看,这是一个愚蠢的错误。 –

+0

添加它作为答案,所以我可以投票:) BenP试图得到的观点:D – Softlion

回答

1

你有应用:layout_constraintRight_toLeftOf。你可能意思是应用程序:layout_constraintRight_toRightOf

1

由于您已指定constraintRight_toLeftOf而不是constraintRight_toRightOf,因此您的视图出现在屏幕外。