在 Intellij IDEA/Android Studio 中使用合并根标记预览布局 [英] Preview layout with merge root tag in Intellij IDEA/Android Studio

查看:29
本文介绍了在 Intellij IDEA/Android Studio 中使用合并根标记预览布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们正在开发基于 LinearLayout 的复合组件.所以,我们创建这样的类:

Let's imagine we are developing compound component based on LinearLayout. So, we create class like this:

public class SomeView extends LinearLayout {
    public SomeView(Context context, AttributeSet attrs) {
        super(context, attrs);

        setOrientation(LinearLayout.VERTICAL);
        View.inflate(context, R.layout.somelayout, this);
    }
}

如果我们使用 LinearLayout 作为 somelayout.xml 的根,我们会有额外的视图级别,所以我们使用合并标签:

If we'll use LinearLayout as a root of somelayout.xml, we'll have extra view level, so we use merge tag:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some text"
        android:textSize="20sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some other text"/>
</merge>

但在 IDE 合并中的预览选项卡中始终充当 FrameLayout,我们将看到类似的内容:

But in Preview tab in IDE merge always acts as FrameLayout, and we'll see something like that:

(是Android Studio,Intellij IDEA也一样,Eclipse我不知道)

(It is Android Studio, Intellij IDEA is just the same, about Eclipse I don't know)

预览大大加快了布局的开发速度,即使对于某些布局也失去了如此大的帮助,这很遗憾.可能有一种方法可以指定,预览应该如何解释特定布局中的 merge 标签?

Preview speed up developing layouts a lot, it's sad lose such a great help even for some layouts. May be there is a way to specify, how Preview should interpret merge tag in particular layout?

推荐答案

有一个新的 parentTag 工具属性 (在 Android Studio 2.2 中添加),您可以使用它来指定合并标签的布局类型,这将使​​布局在布局编辑器预览中正确呈现.

There is a new parentTag tools attribute (added in Android Studio 2.2) that you can use to specify the layout type for a merge tag, which will make the layout render correctly in the layout editor preview.

所以使用你的例子:

<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:parentTag="LinearLayout"
    tools:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some text"
        android:textSize="20sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some other text"/>
</merge>

注意:必须同时指定 android:layout_widthandroid:layout_height 以便布局在编辑器中正确显示.

Note: Both android:layout_width and android:layout_height must be specified in order for the layout to display properly in the editor.

这篇关于在 Intellij IDEA/Android Studio 中使用合并根标记预览布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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