Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

android - sliding drawer appear in all activities

I am developing an application that contains many activities and i created my own menu (i don't want to use the built in menu button) with the Sliding Drawer as the sliding drawer is at the bottom of the screen and contains my menu buttons

what i need is to make that sliding drawer to appear in all my activities

i tried to create an activity and set it's content view to the xml file that includes the drawer and then extends that activity in all other activities but this solution doesn't work

so any suggestions ?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Extending is the right way. Just override setContentView in the right way. Here's the working example, but instead of drawer, I use a created a custom tabbar:

Define a layout with your drawer like this:

this is act_layout.xml

<LinearLayout
  ...
  android:orientation="vertical"
>
  <YourDrawer
    ...
  />
  <FrameLayout
    android:id="@+id/act_content"
    ...
  >
    // Here will be all activity content placed
  </FrameLayout>
</LinearLayout>

This will be your base layout to contain all other layouts in the act_content frame. Next, create a base activity class, and do the following:

public abstract class DrawerActivity extends Activity {

    protected LinearLayout fullLayout;
    protected FrameLayout actContent;

    @Override
    public void setContentView(final int layoutResID) {
        // Your base layout here
        fullLayout= (LinearLayout) getLayoutInflater().inflate(R.layout.act_layout, null); 
        actContent= (FrameLayout) fullLayout.findViewById(R.id.act_content);

        // Setting the content of layout your provided to the act_content frame
        getLayoutInflater().inflate(layoutResID, actContent, true); 
        super.setContentView(fullLayout);

        // here you can get your drawer 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
    }
}

What we do, is basically intercept all calls to setContentView(int resId), inflate our layout for drawer from xml, inflate our layout for activity (by reId provided in method call), combine them as we need, and set as the contentView of the activity.

EDIT: After you've created the stuff above, just proceed to write an app as usual, create layouts (without any mention of a drawer) create activities, but instead of extending simple activity, extend DrawerActivity, like so:

public abstract class SomeActivity extends DrawerActivity {

    protected void onCreate(Bundle bundle) {
        setContentView(R.layout.some_layout);
    }
}

What happens, is that setContentView(R.layout.some_layout) is intercepted. Your DrawerActivity loads the layout you provided from xml, loads a standart layout for your drawer, combines them and then sets it as contentView for the activity.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...