相同的片段导航抽屉在不同的活动中

时间:2016-03-31 13:18:47

标签: android android-fragments

我使用此link在我的应用中创建导航抽屉。问题是:有没有办法创建一个基本的活动女巫有这个抽屉并从中扩展其他活动?我看了this,但所有这些链接都使用了DrawerLayout而不是Fragment Navigation Drawer,我无法使用它们。有没有解决我问题的教程?

1 个答案:

答案 0 :(得分:0)

Here is what I do :

I create an abstrac class called RootActivity that extends Activity and which inflate the layout with the Drawer. This class has an abstract method createPage in which you will inflate your activity layout.

Here is the basic code for the RootActivity :

public abstract class RootActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yourRootlayout); //The root layout wich contain your Drawer

        /**
         * This FrameLayout is used has a container for your activity
         * layout as you would do with a fragment container.
         */
        FrameLayout container = (FrameLayout)findViewById(R.id.yourContainer);

        View childActivityLayout = createPage(savedInstanceState);
        if (childActivityLayout != null) {
            container.addView(childActivityLayout);
        }
    }

    public abstract View createPage(Bundle saveInstanceState);
}

And here is how you extend this root class :

public class ExampleActivity extends RootActivity {
    @Override
    public View createPage(Bundle saveInstanceState) {
        View rootView = ...
        //Inflate your layout
        return rootView;
    }
}
相关问题