简体   繁体   English

Android Master布局模板

[英]Android Master Layout Templating

Hi am am working on android. 嗨我正在研究android。

I have a layout which was used in every activity. 我有一个在每个活动中使用的布局。

I mean I have a layout which has footer and header. 我的意思是我有一个页脚和标题的布局。

On each activity, header and footer same and has same actions. 在每个活动上,页眉和页脚相同并具有相同的操作。

正如你看到的,

I want to use a general layout for header and footer. 我想使用页眉和页脚的一般布局。

I mean in a activity, I will put the content area layout to general layout. 我的意思是在一个活动中,我会将内容区域布局放到一般布局中。

I find someting but not enough. 我发现有些但不够。

How can I do this? 我怎样才能做到这一点?

Is there a dummy docuument for do this?. 这样做是否有虚拟文件?

sorry for bad english. 抱歉英语不好。

What you are talking about is a new Android design pattern called Fragments. 你所说的是一种名为Fragments的新Android设计模式。 Since 3.0 fragments are small activity like views that can be combined to form a screen. 由于3.0片段是像活动一样的小活动,可以组合成一个屏幕。

So you would create a Header and Footer fragment and then include these across all activities that require them. 因此,您将创建一个页眉和页脚片段,然后将其包含在需要它们的所有活动中。

The other pattern you might want to look at is the Action bar pattern, which is used to place a bar at the top of screens with common content and functions, similar to your header. 您可能想要查看的另一种模式是操作栏模式,该模式用于在屏幕顶部放置一个带有常用内容和功能的栏,类似于您的标题。

Also another way would be using xml files to define your header and footer then instantiate these as views in code and add them programmatically to your content views xml definition. 另一种方法是使用xml文件定义页眉和页脚,然后在代码中将它们实例化为视图,并以编程方式将它们添加到内容视图xml定义中。 The problem with this is that the code behind the header and footer would need to be replicated in each controller. 这个问题是页眉和页脚背后的代码需要在每个控制器中复制。 Your best bet is using Fragments, I'll put some useful links below: 你最好的选择是使用Fragments,我会在下面放一些有用的链接:

http://developer.android.com/guide/topics/ui/actionbar.html http://developer.android.com/guide/topics/ui/actionbar.html

http://developer.android.com/guide/topics/fundamentals/fragments.html http://developer.android.com/guide/topics/fundamentals/fragments.html

http://mobile.tutsplus.com/tutorials/android/android-compatibility-working-with-fragments/ http://mobile.tutsplus.com/tutorials/android/android-compatibility-working-with-fragments/

https://stackoverflow.com/questions/5710573/need-a-fragments-example https://stackoverflow.com/questions/5710573/need-a-fragments-example

You could use includes for the header & footer or add them dynamically from a base class, but I think a better approach is to use a single Activity to host the app, and then use Fragments for your screen content. 您可以使用包含在页眉和页脚中,或者从基类动态添加它们,但我认为更好的方法是使用单个Activity来托管应用程序,然后使用Fragments作为屏幕内容。

http://android-developers.blogspot.co.uk/2011/02/android-30-fragments-api.html http://android-developers.blogspot.co.uk/2011/02/android-30-fragments-api.html

I have nothing against fragments, and Yes, they're the way to go, but for the beginner android developer, you can do achieve what you're trying to do with <include> s and base activities. 我没有反对片段,是的,它们是要走的路,但对于初学者Android开发人员,你可以实现你想要用<include>和基础活动做的事情。

This article explains nicely the use of <include> s, but to sum it up, you can have a layout xml file that you can "include" to another layout, instead of rewriting the same stuff over and over. 本文很好地解释了<include>的使用,但总结一下,你可以有一个布局xml文件,你可以“包含”到另一个布局,而不是一遍又一遍地重写相同的东西。

For the functionality of the headers and footers (assuming they do something when clicked), you can create a base activity that you can extend instead of the normal android Activity . 对于页眉和页脚的功能(假设它们在单击时执行某些操作),您可以创建一个可以扩展而不是正常的android Activity Define the logic for the header and footer clicks in this base activity, such as with this sample code: 定义此基本活动中页眉和页脚单击的逻辑,例如使用此示例代码:

public class MyBaseActivity extends Activity {
...
public void onHeaderClick(View view) {
// when header is clicked, do this.
}
public void onFooterClick(View view) {
// when footer is clicked, do this.

In your layout (the one you have as a separate xml), add an onClick attribute to your header/footer, assigning the name of the method in the base activity. 在您的布局(作为单独的xml具有的布局)中,向页眉/页脚添加onClick属性,在基本活动中指定方法的名称。

such as

android:onClick="onHeaderClick"

Then it's just a matter of extending MyBaseActivity for all your activities that have headers and footers. 然后,只需要为所有具有页眉和页脚的活动扩展MyBaseActivity。

看看这个 ,你可以随时重复使用你的布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM