繁体   English   中英

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

[英]Preview layout with merge root tag in Intellij IDEA/Android Studio

让我们假设我们正在开发基于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);
    }
}

如果我们将使用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>

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

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

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

您可以使用新的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>

注意 :两个android:layout_widthandroid:layout_height必须为了使布局在编辑器中正确显示指定。

编辑:过时的答案。 见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>

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>

也可以使用自定义类作为父类而不是像merge一样

<com.mycompany.SomeView xmlns:android="http://schemas.android.com/apk/res/android">
...
</com.mycompany.SomeView>

然后直接膨胀此布局并将结果视图转换为SomeView Android studio将直接检查SomeView父类并处理类似LinerLayout预览。 您可以使用onFinishInflate()方法在SomeView通过绑定意见findViewById() 此解决方案的好处是您可以将所有布局定义或样式定义直接放到布局文件中,您不能在代码中使用setOrientation()等方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM