【问题标题】:Simple example of <merge> and <include> usage in Android XML-layoutsAndroid XML 布局中 <merge> 和 <include> 用法的简单示例
【发布时间】:2011-02-13 12:44:46
【问题描述】:

我很好奇 Android XML 布局中的 &lt;merge&gt;&lt;include&gt; 标记。我已经阅读了两篇教程,但还没有找到一个简单的示例用法。

如果有人能提供这样的例子或指点一下,我会很高兴。

【问题讨论】:

标签: xml android layout merge include


【解决方案1】:

&lt;merge&gt; 标签用于减少层数以提高渲染布局的性能。标签与&lt;include&gt; 标签完美结合使用。

举个例子,我们有一个登录布局,并且在我们的应用范围内用于多个。在使用标签显示 login_layout 时,我们可以使用并且可以转义一个级别。

我还建议您阅读有关布局的技巧。 http://android-developers.blogspot.com.tr/2009/03/android-layout-tricks-3-optimize-by.html

login_form.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Login form -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <EditText
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Email..."
        android:inputType="textEmailAddress"
        android:maxLines="1"
        android:singleLine="true"
        android:visibility="visible" />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Password.."
        android:imeActionId="@+id/login"
        android:imeOptions="actionUnspecified"
        android:inputType="textPassword"
        android:maxLines="1"
        android:singleLine="true"
        android:text="1337"
        android:visibility="visible" />

    <Button
        android:id="@+id/sign_in_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="16sp"
        android:paddingLeft="32sp"
        android:paddingRight="32sp"
        android:text="Login"
        android:visibility="visible" />

</LinearLayout>

example_layout.xml(我们想要包含 login_form.xml 的任何布局)

<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" >

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

</merge>

我们可以看到层次结构

【讨论】:

  • ID 呢?
【解决方案2】:

举个例子:

我有两个标签 &lt;EditText&gt;&lt;ListView &gt; 来自多个 UI。 因此,我创建了一个如下所示的 XML 文件以包含在所有此类 UI 中。

<?xml ...>
<EditText ... />
<ListView ... />   

上面的 XML 不是有效的 XML,因为它没有根元素。 因此,仅仅为了 XML 就需要一个根元素。 &lt;merge&gt; 是下面给出的解决方案:

<?xml ...>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <EditText ... />
    <ListView ... />
</merge>

【讨论】:

  • 感谢您的有用解释 :)
【解决方案3】:

有一个简单的Android XML 布局 HOWTO,它也解释了http://www.coboltforge.com/2012/05/tech-stuff-layout/ 上的一个常见陷阱。这可能会有所帮助...

【讨论】:

    【解决方案4】:

    id 不会粘贴代码,否则相关布局参数会起作用。它做了一些不同的处理

    【讨论】:

      【解决方案5】:

      some_activity.xml

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent" android:layout_height="fill_parent"
          android:orientation="vertical">
      
          // some views
      
          <include layout="@layout/view_part"/>
      
         // probably more views
      
      </LinearLayout>
      

      view_part.xml

      <merge xmlns:android="http://schemas.android.com/apk/res/android">
      
          // the views to be merged
      
      </merge>
      

      【讨论】:

      • 所以合并的东西是由它的文件名引用的...合并文件中没有 id 属性?
      • @aioobe 对。 &lt;include&gt; 基本上意味着“获取该文件并将其内容粘贴到此处”。
      • 嗨,实际上我在这里面临一个严重的问题。我正在使用首选项并指定要在首选项中使用的布局。在布局内部,我正在使用包含合并功能(这样我就有了一个占位符,它将根据版本使用开关或复选框)。问题出在我的preferenceacvitity 的onPostCreate 方法中,当我试图查找视图(即复选框/开关)时,我总是将视图设为null!你能帮忙吗? stackoverflow.com/questions/15708599/…
      • 虽然接受的答案是正确的,但我发现这里的文章:http://mfarhan133.wordpress.com/2010/10/01/reusing-layout-include-merge-tag-for-androd/ 帮助我更好地可视化了它。希望它可以帮助某人。
      • 这可以通过 XML 菜单声明实现吗?
      猜你喜欢
      • 1970-01-01
      • 2012-02-08
      • 2015-06-21
      • 1970-01-01
      • 1970-01-01
      • 2011-01-09
      • 1970-01-01
      • 2014-03-23
      • 2015-05-07
      相关资源
      最近更新 更多