什么是正确的选择呢?创建新的活动或者只是创建一个不同的布局,并取代现有的布局? [英] What's the right choice? Create new Activities or just create a different Layout and replace the existing layout?

查看:102
本文介绍了什么是正确的选择呢?创建新的活动或者只是创建一个不同的布局,并取代现有的布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我是新来的Andr​​oid,我现在想的是什么做的事情的正确方法。

Since I am new to Android, I am now thinking on what is the correct way of doing things.

既然这样,应用程序我写了4个不同的画面:

As it stands, the application Im writing has 4 different screens:

  • 在屏幕1 - 节点列表(主屏幕)
  • 在屏幕2 - 选项菜单,tableLayout的按钮
  • 在屏幕3 - 导航
  • 在屏幕4 - 对版本的文字信息等

这些屏幕可以从利用头查看被放置在顶部导航到/。头部则有4个不同的按钮:

These screens can be navigated to/from using a "header" View that is placed on top. The header then has 4 different buttons:

+--------------------+
| menu with buttons  |
+--------------------+
|                    |
|                    |
|                    |
|  C O N T E N T     |
|                    |
|                    |
|                    |
+--------------------+

的main.xml 是真的只是一个LinearLayout中,包括header.xml然后将内容,在这种情况下的节点列表中的一个ListView

main.xml is really just a LinearLayout that INCLUDES the header.xml and then the content, in that case the list of nodes in a ListView

options.xml 是同样的事情差不多,它包括headerxml,然后一堆按钮...

options.xml is the same thing almost, it includes the headerxml and then a bunch of buttons...

...等与其他两个屏幕。

...and so on with the two other screens.

因此​​,当I $顶部的页眉/菜单中的按钮的对$ PSS的一个内容应被切换到该画面。 我的问题是:

So, when I press one of the buttons in the header/menu on top the content should be switched to that screen. My question is:

  • 我应该创建一个活动为每个屏幕?我读了谷歌认为:
    一个活动presents了一个可视化用户界面集中努力,用户可以进行。
    以便可以是我shoukd使用一个活动的每个这些屏幕间preTET

  • Should I create one Activity for each screen? I read on Google that:
    An activity presents a visual user interface for one focused endeavor the user can undertake.
    So that can be interpretet that I shoukd use one Activity for each of these screens.

我应该不是创造出比启动更多的活动,然后只需运行的setContentView(R.layout.whatever)时,我想改变内容上面?

Should I not create more Activities than the startup, and then just run the setContentView(R.layout.whatever) when I want to change the "content" above?

问候

推荐答案

您应该使用单独的活动每个屏幕;否则,你需要结束跟踪个别查看当前显示的,加上那些当前显示不是当用户切换到另一个窗口,或者一个国家电话打进来等。

You should probably use a separate Activity for each screen; otherwise you need to end up keeping track of which individual View is currently being displayed, plus the state of all those not currently being displayed when the user switches to another window, or a call comes in etc.

这是更容易保持这种状态的轨道,如果你只需要使用一个单独的活动,每件的功能。

It's easier to keep track of this state if you just use a separate Activity for each piece of functionality.

如果你决定把一切都在一个活动不过,你可以看看<一href="http://developer.android.com/reference/android/app/TabActivity.html"><$c$c>TabActivity类。不过,也有告诫那里,prevent你有一个活动作为标签内容。

If you do decide to keep everything in a single Activity however, you could look at the TabActivity class. However, there are also caveats there that prevent you from having an Activity as the tab content.

  • <一个href="http://stackoverflow.com/questions/1568739/android-why-shouldnt-i-use-activities-inside-tabs">http://stackoverflow.com/questions/1568739/android-why-shouldnt-i-use-activities-inside-tabs

关于你的跟进,你可惜不能附加一个意图直接将按钮像你可以用菜单项通过XML,但是你可能只是延长活动一些code,使自己的公共基类挂钩了听众。

Regarding your follow-up, you unfortunately cannot attach an Intent directly to a Button like you can with a MenuItem via the XML, however you could just extend Activity to make your own common base class with some code that hooks up the listeners.

是这样的:

public class BaseActivity extends Activity {
    protected View.OnClickListener mButtonListener;

    protected void setupHeaderButtons() {
        findViewById(R.id.header_btn_1).setOnClickListener(mButtonListener);
        // ...
        findViewById(R.id.header_btn_n).setOnClickListener(mButtonListener);
    }
}

public class FirstActivity extends BaseActivity {
    @Override
    public void onCreate(Bundle b) {
        super.onCreate(b);
        setContentView(R.layout.first_activity);

        // This needs to be done *after* the View has been inflated
        setupHeaderButtons();
    }
}

这篇关于什么是正确的选择呢?创建新的活动或者只是创建一个不同的布局,并取代现有的布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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