使用< include>与< merge>在ConstraintLayout中 [英] Using <include> with <merge> in ConstraintLayout

查看:116
本文介绍了使用< include>与< merge>在ConstraintLayout中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ConstraintLayout中使用标签<include><merge>时遇到麻烦.

I am having trouble using tags <include> and <merge> inside a ConstraintLayout.

我想创建一个平面视图层次结构(因此受约束),但是仍然具有可重用的元素.因此,在布局中使用<include>,在包含的布局中使用<merge>,以避免使用嵌套的布局(特别是避免使用嵌套的ConstraintLayouts)

I want to create a flat view hierarchy (hence Constraints) but still have elements that are reusable. So I use <include> in my layout and <merge> in the included layouts to avoid having nested layouts (especially avoiding nested ConstraintLayouts)

所以我写了这个: 父级布局

So I wrote this: Parent layout

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/review_1"
        layout="@layout/view_movie_note"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/review_2"/>

    <include
        layout="@layout/view_movie_note"
        android:id="@+id/review_2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginLeft="7dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/review_1"
        app:layout_constraintRight_toRightOf="parent"
        />

</android.support.constraint.ConstraintLayout>

和这个view_movie_note:

and this view_movie_note :

<merge>

    <TextView
        android:id="@+id/note_origin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15dp"
        android:layout_marginStart="5dp"
        app:layout_constraintStart_toStartOf="@+id/cardView2"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginLeft="5dp" />


    <android.support.v7.widget.CardView
        android:id="@+id/five_star_view_container"
        android:layout_width="0dp"
        android:layout_height="52dp"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="10dp"
        android:elevation="3dp"
        app:cardUseCompatPadding="true"
        app:contentPaddingTop="22dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_min="52dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/note_origin">

        <FiveStarsView
            android:id="@+id/five_star_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal" />

    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:id="@+id/cardView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        app:cardBackgroundColor="@color/colorPrimary"
        app:contentPaddingLeft="15dp"
        app:contentPaddingRight="15dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/note_origin">

        <TextView
            android:id="@+id/grade"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp" />

    </android.support.v7.widget.CardView>


</merge>

我希望如此

相反,我得到了这个

很明显,我在<include>标记中放置的约束被包含的布局中的约束覆盖.

Clearly the constraints that I put in the <include> tag are overriden by the constraints in the included layout.

这是预期的行为吗?如果是,我们应该如何使用<include>和ConstraintLayout保持平坦的布局?

Is this the expected behaviour ? If yes, how are we supposed to keep a flat layout using <include> and ConstraintLayout ?

推荐答案

简短答案

最好的方法是用(嵌套的)ConstraintLayout 代替<merge>块,而不是使用多余的布局结构.

Short answer

The best move will be replacing <merge> block with a (nested) ConstraintLayout rather than using redundant layout structure.







ConstraintLayout很棒,但是在合成时效果不佳 和每件责任的分离

ConstraintLayout is great but it doesn't work well with composition and separation of responsibilities of each piece

那是错误的. ConstraintLayout与重用布局效果很好.任何根据同级视图和父级布局之间的关系对所有子视图进行布局的布局都具有完全相同的行为.即使对于RelativeLayout也是这样.

That is wrong. ConstraintLayout does work well with reusing layouts. Any layout in which all child views are laid out according to relationships between sibling views and the parent layout, behaves exactly like this. This is true even for RelativeLayout.


让我们仔细看看 <merge> 是.

Let's take a closer look at what <merge> is.

医生说

<merge/>标记有助于消除视图中的多余视图组 将一种布局包含在另一种布局中时的层次结构.

The <merge/> tag helps eliminate redundant view groups in your view hierarchy when including one layout within another.

与将<include>元素替换为<merge>块的内容具有相同的效果.换句话说,<merge/>块中的视图直接放置在父布局中,而没有中间视图组.因此,<include>元素的约束被完全忽略.

It will have the same effect as replacing the <include> element with the contents of <merge> block. In other words, the views in the <merge/> block is directly placed to the parent layout without an intermediate view group. Therefore, the constraints of the <include> element is completely ignored.

在此特定示例中,包含布局中的视图两次添加到父视图中,作为第二个视图添加到另一个视图之上.

In this particular example, the views in the including layout is added two times to the parent as the second one on top of another.


布局资源文件旨在独立使用.要限定术语可重用",它不应依赖于其父级(将来将在其中添加的视图组). 如果只需要一次包含布局,那看起来就可以了.但是</merge>在那种情况下也不是一个好主意,因为您不能将其放置在任何不同布局中的不同位置.

Layout resource files are intended to be used independently. To qualify the term reusable, it should not depend on it's parent (The view group in which it will be added in future). It would be looking okay if you had to include the layout only one time. But </merge> won't be a good idea in that case too because you can't place it in any different layout in a different position.

显然,平面布局层次结构具有更好的性能.但是,有时我们可能不得不牺牲它.

Obviously, flat layout hierarchies have better performance. However, sometimes we may have to sacrifice it.

这篇关于使用&lt; include&gt;与&lt; merge&gt;在ConstraintLayout中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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