引用另一个 layout.xml 文件而不复制它 [英] Referencing another layout.xml file without duplicating it

查看:44
本文介绍了引用另一个 layout.xml 文件而不复制它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为多个不同限定符的 Activity 提供相同的 layout.xml 文件.我知道有一种方法可以只引用一个现有的 layout.xml,而不是真正复制它并复制一个.

I need to provide the same layout.xml file for an Activity for several different qualifiers. I know that there's a way to just reference one existing layout.xml instead of really copying it and having a duplicate one.

但是怎么样?现在在 Android 文档中找不到它... :-/

But how? Can't find it in the Android docs right now... :-/

有人比我快吗?

虽然我找到了这个解决方案",但我仍然不在那里.

although I found this "solution" I am still not there.

<?xml version="1.0" encoding="utf-8"?>
<merge>
    <include layout="@layout/main_ltr"/>
</merge>

我需要指向不同的限定符的布局文件,而不是指向同一限定符中的另一个布局文件.背后的原因:我通过证明屏幕宽度限定符指定了新的 Android 3.2 限定符.但是在 Android 3.0/3.1 上这不起作用,我需要 xlarge,但我希望它是完全相同的文件,而不是副本!

I need to point to a different qualifiers's layout file, not to another layout file in the same qualifier. Reason behind it: I specified the new Android 3.2 qualifier by proving screen width qualifiers. But on Android 3.0/3.1 this does not work, I need xlarge there, but I want it to be exactly the SAME file, not a copy!

推荐答案

如果我理解正确,asker 有一个用于 xlarge 和 sw-600dp 的布局文件,另一个用于其他所有布局.无论如何,当我偶然发现这个问题时,这就是我的情况.

If I understood correctly asker has one layout file for xlarge and sw-600dp and another one for all the rest. Anyway that was my situation when I stumbled on this question.

可以通过创建文件夹 layout-xlarge 和 layout-s600dp 并在每个文件夹中放置一个布局文件但内容相同来解决此问题.但出于显而易见的原因,人们不希望两个文件夹中有两个完全相同的文件.

One can solve this by creating folders layout-xlarge and layout-s600dp and put one layout file in each but with the same contents. But one would like not to have two exact same files in two folders for obvious reasons.

这是我对问题的解决方案.根本不要制作 layout-xlarge 和 layout-sw600dp 文件.假设您正在使用布局文件/res/layout/cool_activity.xml 创建一个很酷的活动.在同一个文件夹中创建您的平板电脑布局文件,但名称不同,即/res/layout/cool_activity_for_tablet.xml,然后创建文件夹 values-xlarge 和 values-sw600dp,其中包含 layout.xml 文件,内容如下

This is my solution for the problem. Dont make layout-xlarge and layout-sw600dp files at all. Let's say you're creating a cool activity with layout file /res/layout/cool_activity.xml. Create your tablet layout file in the same folder but with a different name, i.e. /res/layout/cool_activity_for_tablet.xml and then create folders values-xlarge and values-sw600dp with layout.xml files in it with the following content

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="cool_activity" type="layout">@layout/cool_activity_for_tablet</item>
</resources>

是的,您仍然会有 2 个具有相同内容的内容,但这是愚蠢的内容,而不是布局本身,可能是数百行 xml.

Yes you will still have 2 of these with the same content but this is dumb content not the layout itself which can be hundreds of lines of xml.

这篇关于引用另一个 layout.xml 文件而不复制它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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