3

我在名为 intro_step1_activity.xml 的文件中定义了一个 LinearLayout。我想在另一个 xml 文件中加载/引用这个文件,这样我就不需要在另一个 xml 文件中重新键入代码。这可能吗?如果可以,怎么做?

4

4 回答 4

4

在运行时,您可以使用充气机,例如:

Inflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.intro_step1_activity, null);

然后您可以将此视图添加到当前视图层次结构中。在编译时,您可以使用 xml 的 include 标记

<include layout="@layout/intro_step1_activity"/>
于 2013-07-30T19:01:02.537 回答
3
Load an existing xml layout file in another xml layout file

在其他 xml 文件中使用 xml 文件<include />

供您intro_step1_activity.xml使用代码

<include layout="intro_step1_activity.xml"/>
于 2013-07-30T19:02:52.253 回答
0

是的,可以使用<merge>or <include>

你可以在这里这里阅读更多

于 2013-07-30T19:02:15.240 回答
0

首先给它一个idLinearLayout然后LinearLayout在java中定义一个并使用该findViewById()方法引用它。

然后使用LinearLayout您刚刚创建的对象来调用该addView()方法。

如果你想要一个例子,评论。

于 2013-07-30T19:01:55.453 回答