在Intellij IDEA/Android Studio中使用合并根标签预览布局

dar*_*rja 146 android intellij-idea android-layout android-studio android-tools-namespace

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

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

        setOrientation(LinearLayout.VERTICAL);
        View.inflate(context, R.layout.somelayout, this);
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我们将使用它LinearLayout作为根somelayout.xml,我们将有额外的视图级别,所以我们使用merge标记:

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

但是在IDE中的预览选项卡中,合并始终充当FrameLayout,我们会看到类似的东西: 合并预览

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

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

sta*_*ej2 317

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

所以使用你的例子:

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

注意:必须指定两者android:layout_width,android:layout_height以便在编辑器中正确显示布局.

  • 这就像一个魅力.谢谢.+1 (2认同)
  • 由于您使用的是工具,因此您也可以使用工具:layout_height="match_parent" (2认同)

Jon*_*nas 63

编辑:过时的答案.见starkej2的回答.


Android Studio 0.5.8增加了对工具的支持:showIn.通过使用它,可以预览<merge>布局.

http://tools.android.com/recent/androidstudio058released

layout/layout_merge.xml with tools:showIn:

<merge xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:custom="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   tools:showIn="@layout/simple_relativelayout">

......

</merge>
Run Code Online (Sandbox Code Playgroud)

layout/simple_relativelayout.xml with include:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include layout="@layout/layout_merge"/>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

  • 好消息!复合组件不是很方便,因为我们只需要为预览添加额外的布局.但总比没有好. (13认同)
  • 您可以关注由Google开发人员在此处报告的故障单:https://code.google.com/p/android/issues/detail?id = 61652 (3认同)