Android是否支持布局继承(类似于Django模板)? [英] Does Android support layout inheritance (similar to Django templates)?

查看:137
本文介绍了Android是否支持布局继承(类似于Django模板)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能会误会< include> < merge> 的工作,但根据< merge>的简单示例和< include>在Android XML布局中的使用< include> 意味着取该文件并将其内容粘贴到此处。



假设你想有3个活动都有相同的标题,但内容不同。您仍然需要为每个活动拥有3个XML布局文件。每个布局的唯一区别是它们在< include> 标签中定义了不同的布局。如果我想添加一个页脚,我必须更改每个布局。



我正在寻找一种实现逆向的方法,因此子版面将覆盖特定父项的块(类似于Django模板)。这样,如果我想添加该页脚,我只是更改父级,孩子们将继续覆盖内容。

解决方案

假设你想要有3个活动都有相同的标题,但内容不一样。您仍然需要为每个活动拥有3个XML布局文件。每个布局之间的唯一区别是它们在标签中定义了不同的布局。


如果要进行三个活动所有这些都具有相同的标题,每个活动将有一个布局文件。该文件将具有该活动唯一的小部件,以及公用标题的< include> 元素。


这样,如果我想添加该页脚,我只是更改父级,孩子们将继续覆盖内容。


目前Android不支持此功能。正如janoliver指出的那样,您可以为此自行解决。


I may be misunderstanding how <include> and <merge> work, but according to Simple example of <merge> and <include> usage in Android XML-layouts, <include> means "take that file and paste its contents here".

Let's say you want to have 3 activities that all have the same header, but have different content. You'll still need to have 3 XML layout files for each activity. The only difference between each of the layout's will be that they define a different layout in the <include> tag. If I wanted to add a footer I'd have to change each layout.

I'm looking for a way to achieve the inverse, so the children layouts would override specific blocks of the parent (similar to Django templates). That way, if I wanted to add that footer, I would just change the parent and the children would continue to override just the content.

解决方案

Let's say you want to have 3 activities that all have the same header, but have different content. You'll still need to have 3 XML layout files for each activity. The only difference between each of the layout's will be that they define a different layout in the tag.

If you want to have three activities that all have the same header, you will have one layout file per activity. That file will have the widgets unique to that activity, and an <include> element for the common header.

That way, if I wanted to add that footer, I would just change the parent and the children would continue to override just the content.

That is not supported by Android at this time. As janoliver indicates, you could roll your own solution for this.

这篇关于Android是否支持布局继承(类似于Django模板)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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