2 layouts 1 activity android

I have a navigation bar in my application, the fact is that I want the navigator to be available in all actions. I suppose I should set the contentView two times, but this does not work, of course.

I watched, but I am not going to work. I have a superclass, can I install this second layout from my superclass?

+3
source share
3 answers

You must enable the navigation bar through the tag <include>from other layouts. Setting the content layout twice will not work, since Android is in callbacks, mostly always using what the user said. So,

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

will only use the main layout.

, include.

+4

(Activity, ListActivity .., - ) nav_bar.

:

nabar,

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

, nav_content. :

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
    }
}

, , , NavActivity. , , ( ).

+2

Source: https://habr.com/ru/post/1789945/


All Articles