2

我在编辑包含Relativelayout的Cardview内部的RelativeLayout时搞砸了! ConstraintLayout会将相对布局的wrap_content更改为0并添加工具:layout_editor_absoluteX =“10dp”内部的所有文本视图相对布局将折叠卡片视图的右上方! (嵌套CardView元素获取CardView之外对准元件)设计CardView虽然它的父母是ConstraintLayout?

<android.support.v7.widget.CardView 
    android:id="@+id/card_view" 
    android:layout_gravity="center" 
    android:layout_width="357dp" 
    android:layout_height="114dp" 
    android:layout_margin="5dp" 
    app:cardCornerRadius="2dp" 
    app:contentPadding="10dp" 
    app:cardElevation="10dp" 
    tools:layout_editor_absoluteY="36dp" 
    app:cardBackgroundColor="@android:color/white" 
    tools:layout_editor_absoluteX="11dp"> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     tools:layout_editor_absoluteX="10dp" 
     tools:layout_editor_absoluteY="10dp"> 

     <TextView 
      android:text="TextView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/textView2" 
      tools:text="Location" 
      android:textAppearance="@style/TextAppearance.AppCompat.Body2" 
      tools:layout_editor_absoluteX="0dp" 
      tools:layout_editor_absoluteY="0dp" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" /> 

    </RelativeLayout> 

    <ImageView 
     android:src="@drawable/ic_hotel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageView3" 
     tools:layout_editor_absoluteX="0dp" 
     tools:layout_editor_absoluteY="0dp" 
     android:layout_centerVertical="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

    <TextView 
     android:text="TextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView3" 
     tools:text="Bangalore" 
     android:textAppearance="@style/TextAppearance.AppCompat.Medium" 
     tools:layout_editor_absoluteX="10dp" 
     tools:layout_editor_absoluteY="10dp" 
     android:layout_alignBottom="@+id/imageView3" 
     android:layout_toRightOf="@+id/textView2" 
     android:layout_toEndOf="@+id/textView2" /> 
</android.support.v7.widget.CardView> 

关于与ConstraintLayout设计的RelativeLayout将是巨大的充满任何指导,不能用的RelativeLayout同时使用ConstraintLayout?在CardViews是ConstraintLayout的子项时,使用RelativeLayout时使用CardView的子项的guidline是什么?

回答

9

其实,这不是问题跟踪意味着什么: -/

把它清除掉,插在ConstraintLayout(甚至非嫡系)的任何儿童tools:layout_editor_absolute[X/Y]属性为过Android Studio 2.2预览的错误(自相信3或4以来,它已被修复)。也就是说,他们除了在场之外没有其他任何东西,因为除了ConstraintLayout之外,其他任何东西都不会对他们做任何事情。

现在,CardView本身 - 有没有需要一个嵌套ConstraintLayout - CardView是一个简单的FrameLayout里,所以如果你想使用ConstraintLayout里面,一切正常。

与此同时,您可以将一个CardView放入一个ConstraintLayout中,这也起作用。

问题跟踪器要求的是不同的 - 它要求在ConstraintLayout > CardView > TextView的层次结构上对TextView应用约束。但这不是android布局系统的工作方式,这就是为什么问题以WorkingAsIntended方式关闭的原因。

当您在此处添加属性到TextView时,负责理解这些属性的布局只能是其直接父级 - 在本例中为CardView。并且CardView不是ConstraintLayout的子类,它是FrameLayout的子类 - 基本上,它不知道如何处理插入的那些ConstraintLayout属性。

你可以做的,而不是像一个层次:

ConstraintLayout > CardView > ConstraintLayout > TextView 

TextView的然后可以使用约束定位,因为这将是一个ConstraintLayout ViewGroup中的直系后裔。

enter image description here

+0

感谢,对于详细的答复!我会给它一个尝试与预览3或4! –

+1

beta 1已经发布,但可能是一个更好的主意:) http://tools.android.com/recent/androidstudio22betaavailable –

+8

好的,感谢您的解释。但是在那种情况下,ConstraintLayout如何在布局中解析深层次结构?看起来和嵌套LinearLayouts一样糟糕...... – Radu