android - 警告 : This <FrameLayout> can be replaced with a <merge> tag

标签 android xml android-framelayout android-lint

我有一个 FrameLayout包含 TextView和两个 LinearLayout年代:

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


    ... a textview and 2 linearlayouts


</FrameLayout>

运行 Android Lint 后,我​​收到以下警告:This <FrameLayout> can be replaced with a <merge> tag.

为什么会出现此警告?我能做些什么来修复它(除了忽略)?

最佳答案

要理解这一点,您需要了解布局是如何膨胀和放置的。

例如,假设您有一个 Activity ,这是您使用的布局 xml。这是放置布局文件之前 Activity 的布局。

<FrameLayout> // This is the window
    <FrameLayout> // This is activity
    </FrameLayout>
</FrameLayout>

根据设备/操作系统,可能还有一些其他层。

现在,当您扩展布局文件并将其放入时,它的外观就是这样。

<FrameLayout> // This is the window
    <FrameLayout> // This is activity
            //A textview and 2 linearlayouts
    </FrameLayout>
</FrameLayout>

你看到另一个FrameLayout里面的FrameLayout了吗?拥有它是多余的,因为它不会增加太多值(value)。要进行优化,您可以将 outer FrameLayout 替换为 <merge>标签。这就是它的样子。

<merge> // This is the window
    <FrameLayout> // This is activity
            //A textview and 2 linearlayouts
    </FrameLayout>
</merge>

注意没有额外的 FrameLayout。相反,它只是与 Activity 的 FrameLayout 合并。只要有可能,您应该使用 <merge> .这不仅适用于 FrameLayouts。你可以在这里读更多关于它的内容。 http://developer.android.com/training/improving-layouts/reusing-layouts.html#Merge

希望这会有所帮助。

关于android - 警告 : This <FrameLayout> can be replaced with a <merge> tag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21488027/

相关文章:

java - 无法实例化 Activity ComponentInfo{ProductSimpleInfoActivity} NullPointerException

android - Android 中的 HttpPost - 无法识别的字符

java - 我什么时候应该选择 SAX 而不是 StAX?

android - 使用 FrameLayout 将 TextView 置于 ImageView 之上

android - 通过 URL 更改 Framelayout 的背景图像

android - 居中裁剪图像以适当的尺寸设置在 ImageView 上

android - 如何强制 PhoneGap Android 应用程序执行 self 更新?

android - 在 android 4 中使用 Asynctask 和 webservice 自动完成 TextView

python - 使用ElementTree逐一解析目录下的所有xml文件

java - Android 中使用 XMLPullParser 解析本地 XML 文件