在设计CardView时将其作为ConstraintLayout? [英] Designing CardView while it's Parent is ConstraintLayout?

查看:125
本文介绍了在设计CardView时将其作为ConstraintLayout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I messed up while editing RelativeLayout inside Cardview that contains Relativelayout ! ConstraintLayout will change wrap_content of relative layout to 0 and adds tools:layout_editor_absoluteX="10dp" all textview inside Relative layout will collapse top right of the cardview !!! (element in the nested CardView gets aligned to the element outside of the 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>

关于使用ConstraintLayout设计RelativeLayout的任何指南都非常完善,在使用ConstraintLayout时不能在任何地方使用RelativeLayout吗?当CardViews是ConstraintLayout的子级时,在使用RelativeLayout时使用CardView的子级的准则是什么?

any guide regarding designing RelativeLayout with ConstraintLayout would be great full, RelativeLayout can't be used anywhere while using ConstraintLayout? what is the guidline to use children of a CardView while using RelativeLayout while CardViews is a children of ConstraintLayout?

推荐答案

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

Actually, that's not what the issue tracker implies :-/

为了清除它,在ConstraintLayout的任何子代(甚至是非直接后代)中插入的tools:layout_editor_absolute[X/Y]属性是Android Studio 2.2预览版的错误(我相信,自预览版3或4以来已修复).就是说,他们实际上只是在场而已,因为ConstraintLayout不会对他们做任何其他事情.

To clear it up, the tools:layout_editor_absolute[X/Y] attributes inserted in any children of a ConstraintLayout (even non-direct descendant) was a bug of Android Studio 2.2 preview (it's been fixed since preview 3 or 4 I believe). That said, they did literally nothing other than being present, as anything else than ConstraintLayout wouldn't do anything with them.

现在,对于CardView本身-不需要 Nested ConstraintLayout -CardView只是一个FrameLayout,因此,如果您想在其中使用ConstraintLayout,那么一切正常.

Now, for CardView itself -- there are no need for a Nested ConstraintLayout -- CardView is simply a FrameLayout, so if you want to use a ConstraintLayout inside it, everything works.

同时,您可以将CardView放在ConstraintLayout内,也可以使用.

In parallel, you can put a CardView inside a ConstraintLayout, that works too.

问题跟踪程序要求的是不同的-它要求的层次结构是ConstraintLayout > CardView > TextViewTextView上施加约束.但这不是android布局系统的工作方式,这就是为什么该问题以WorkingAsIntended的形式关闭了.

What the issue tracker was asking for was different -- it was asking, in a hierarchy such that ConstraintLayout > CardView > TextView to apply constraints on the TextView. But that's not how the android layout system works, which is why the issue was closed as WorkingAsIntended.

在此处将属性添加到TextView时,负责理解这些属性的布局只能是其直接父级-在这种情况下为CardView.而且CardView不是ConstraintLayout的子类,它是FrameLayout的子类-基本上,它不知道如何处理插入的ConstraintLayout属性.

When you add attributes to TextView here, the layout responsible to understand those attributes can only be its direct parent -- in this case, CardView. And CardView is not a subclass of ConstraintLayout, it's a subclass of FrameLayout -- basically, it doesn't know what to do with those ConstraintLayout attributes inserted.

相反,您可以做的是一个类似以下的层次结构:

What you could do instead is a hierarchy like:

ConstraintLayout > CardView > ConstraintLayout > TextView

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

The TextView could then be positioned using constraints, as it would be a direct descendant of a ConstraintLayout viewgroup.

这篇关于在设计CardView时将其作为ConstraintLayout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆