设计CardView而它的父是ConstraintLayout?

LOG*_*TAG 6 android android-layout android-cardview android-constraintlayout

我在Cardview中编辑包含Relativelayout的RelativeLayout时搞砸了!ConstraintLayout将相对布局的wrap_content更改为0并添加工具:layout_editor_absoluteX ="10dp"所有textview里面相对布局将折叠在cardview的右上方!(嵌套的CardView中的元素与CardView外部的元素对齐)

<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>
Run Code Online (Sandbox Code Playgroud)

任何有关使用ConstraintLayout设计RelativeLayout的指南都会很好,在使用ConstraintLayout时,RelativeLayout不能在任何地方使用?当CardViews是ConstraintLayout的子代时,使用RelativeLayout时使用CardView的子代的指导是什么?

Nic*_*ard 16

实际上,这不是问题跟踪器所暗示的: - /

要清除它,在tools:layout_editor_absolute[X/Y]ConstraintLayout的任何子项(甚至非直接后代)中插入的属性都是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
Run Code Online (Sandbox Code Playgroud)

然后可以使用约束来定位TextView,因为它将是ConstraintLayout视图组的直接后代.

在此输入图像描述

  • 好的,谢谢你的解释.但那么在那种情况下,ConstraintLayout如何解决布局中的深层次结构?看起来和嵌套LinearLayouts一样糟糕...... (13认同)
  • 我为此使用的一种技巧是在单独的布局中设计 cardview,然后通过复制粘贴 xml 代码或使用包含将其添加到当前层次结构。这将约束和视图分开。 (2认同)