【问题标题】:Include layout file into constraint layout without affecting performance or Android Studio previews在不影响性能或 Android Studio 预览的情况下将布局文件包含到约束布局中
【发布时间】:2020-05-09 12:22:54
【问题描述】:

我有一个巨大的布局文件,其中包含一个平面约束布局。

我有相同的android.support.constraint.Group 元素。我想将它们移动到一个单独的文件中,然后像 <include layout="@layout/selection_group"/> 那样包含它们

我面临的问题是我在文件selection_group.xml 中的组在 Android Studio 中没有正确预览。有没有办法让 Android Studio 直接在文件中预览它或以不同的方式包含它们?

selection_group.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <android.support.constraint.Group
        android:id="@+id/top_bar_config_one_background_group"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/top_bar_background_with_border_fx"
        android:clickable="true"
        app:constraint_referenced_ids="top_bar_config_one,top_bar_tooth_one"
        app:layout_constraintBottom_toBottomOf="@+id/top_bar_container_background"
        app:layout_constraintLeft_toLeftOf="@+id/top_bar_container_background"
        app:layout_constraintRight_toLeftOf="@+id/top_bar_config_two"
        app:layout_constraintTop_toTopOf="@+id/top_bar_container_background" />

    <ImageView
        android:id="@+id/top_bar_config_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:scaleType="center"
        android:src="@drawable/ic_height_over_sea_100x26"
        app:layout_constraintBottom_toTopOf="@+id/top_bar_tooth_one"
        app:layout_constraintLeft_toLeftOf="@+id/top_bar_container_background"
        app:layout_constraintRight_toLeftOf="@+id/top_bar_config_two"
        app:layout_constraintTop_toTopOf="@+id/top_bar_config_one_background_group" />

    <ImageView
        android:id="@+id/top_bar_tooth_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="center"
        android:src="@drawable/ic_tooth_auto_40x40"
        app:layout_constraintBottom_toBottomOf="@+id/top_bar_config_one_background_group"
        app:layout_constraintLeft_toLeftOf="@+id/top_bar_container_background"
        app:layout_constraintRight_toLeftOf="@+id/top_bar_config_two" />
    <?xml version="1.0" encoding="utf-8"?>
</layout>

我不想将多个约束布局嵌套在一起。我认为selection_group.xml 中的根&lt;layout&gt; 视图已经过优化,不会影响性能?我的目标是减少冗余代码而不影响性能

【问题讨论】:

标签: android android-constraintlayout


【解决方案1】:

尝试在包含的布局中添加 merge 标签:

<layout>
    <merge>
    <group>
    ... etc. ...
    </merge>
</layout>

【讨论】:

  • 合并正是我所需要的。我想,右键单击重构-> 布局为我做了这个。不需要外部 标签。
【解决方案2】:

为了准备好包含该布局文件,您需要将 &lt;layout&gt; 替换为 &lt;merge&gt;

如果您需要在布局中使用 DataBinding,您需要首先在布局文件顶部添加 &lt;layout&lt;merge&gt; 标签

我命令在您的内部布局中进行适当的预览,这将被包括在内 - 在顶层添加到合并标签中:

工具:parentTag="ContraintLayout"

类似这样的:

<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout">

【讨论】:

    猜你喜欢
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-16
    • 2018-12-03
    • 2016-10-15
    • 1970-01-01
    相关资源
    最近更新 更多