简体   繁体   English

2个布局1活动android

[英]2 layouts 1 activity android

I have a navigation bar in my app, the thing is that I want the navbar to be available in all activities. 我的应用程序中有一个导航栏,问题是我希望导航栏可用于所有活动。 I suppose that I have to set contentView two times, but that doesn't work, of course. 我想我必须设置contentView两次,但当然这不起作用。

I've been looking at and but I dont get it to work. 我一直在看,但我不能让它工作。 I have a super class, can I set this second layout from my super class? 我有一个超级课程,我可以从我的超级课程中设置第二个布局吗?

You should include the nav bar via <include> tag from the other layouts. 您应该通过其他布局中的<include>标记包含导航栏。 Setting the content layout twice will not work, as Android is in the callbacks basically always using what the user has told last. 设置两次内容布局是行不通的,因为Android在回调中基本上总是使用用户最后说的内容。 So 所以

setContentLayout(R.layout.nav);
setContentLayout(R.layout.main);

will result in only the main layout being used. 将导致仅使用主要布局。

Have a look at this article which gives an example of using the include tag. 看一下这篇文章 ,它给出了使用include标签的一个例子。

You can extend standard activities (Activity, ListActivity, etc.. if you use any others) and use them as a base for including a nav_bar. 您可以扩展标准活动(Activity,ListActivity等等,如果您使用其他任何活动)并将其用作包含nav_bar的基础。

For example: 例如:

Define a layout with nabar like this 像这样用nabar定义布局

<LinearLayout
  ...
  android:orientation="vertical"
>
  <YourNavBarComponent
    ...
  />
  <FrameLayout
    android:id="@+id/nav_content"
    ...
  >
    // Leave this empty for activity content
  </FrameLayout>
</LinearLayout>

This will be your base layout to contain all other layouts in the nav_content frame. 这将是您的基本布局,以包含nav_content框架中的所有其他布局。 Next, in create a base activity class, and do the following: 接下来,在创建基本活动类时,执行以下操作:

public abstract class NavActivity extends Activity {

    protected LinearLayout fullLayout;
    protected FrameLayout navContent;

    @Override
    public void setContentView(final int layoutResID) {
        fullLayout= (LinearLayout) getLayoutInflater().inflate(R.layout.nav_layout, null); // Your base layout here
        navContent= (FrameLayout) fullLayout.findViewById(R.id.nav_content);
        getLayoutInflater().inflate(layoutResID, navContent, true); // Setting the content of layout your provided in the nav_content frame
        setContentView(fullLayout);
        // here you can get your navigation buttons and define how they should behave and what must they do, so you won't be needing to repeat it in every activity class
    }
}

And now, when you create a new activity, where you need a nav bar, just extend NavActivity instead. 现在,当您创建一个需要导航栏的新活动时,只需扩展NavActivity即可。 And your nav bar will be placed where you need it, without repeating it in every layout over and over again, and polluting the layouts (not to mention repeating a code to control navigation in every activity class). 并且您的导航栏将被放置在您需要的位置,而不是一遍又一遍地在每个布局中重复,并污染布局(更不用说重复代码来控制每个活动类中的导航)。

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

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