创建新的Activity还是仅创建其他布局并替换现有布局会更好吗? [英] Is it better to create new Activities or just create a different Layout and replace the existing layout?

查看:72
本文介绍了创建新的Activity还是仅创建其他布局并替换现有布局会更好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我是Android新手,所以我现在正在考虑正确的做事方式.

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 I'm 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.

因此,当我按顶部的标题/菜单中的按钮之一时,内容应切换到该屏幕.我的问题是:

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:

  • 我应该为每个屏幕创建一个活动吗?我在Google上阅读到:
    一个活动展示了一个可视化的用户界面,供用户集中精力进行.因此可以解释为,我在每个屏幕上都应使用一个活动".

  • 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 interpreted that I should use one Activity for each of these screens.

我应该创建比启动更多的活动,然后在要更改内容"上方?

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?

推荐答案

您可能应该为每个屏幕使用单独的 Activity ;否则,您最终需要跟踪当前正在显示哪个 View ,以及当用户切换到另一个窗口或打进电话等时当前未显示的所有状态.

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.

如果为每个功能使用单独的 Activity ,则更容易跟踪此状态.

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

但是,如果您决定将所有内容都保存在一个 Activity 中,则可以查看

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.

关于后续行动,很遗憾,您无法像通过XML的 MenuItem 一样,将 Intent 直接附加到 Button 上,但是,您可以扩展 Activity 来使用一些连接侦听器的代码来创建自己的通用基类.

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();
    }
}

这篇关于创建新的Activity还是仅创建其他布局并替换现有布局会更好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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