滑动抽屉出现在所有活动 [英] sliding drawer appear in all activities

查看:140
本文介绍了滑动抽屉出现在所有活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发包含许多活动的应用程序 我创建了自己的菜单(我不希望使用内置的菜单按钮)与滑动抽屉 作为滑动抽屉位于屏幕的底部,并包含我的菜单按钮

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

我试图创建一个活动,并设置它的内容以包括抽屉中的XML文件,然后扩展了所有其他活动活动,但这种解决方案不起作用

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

所以有什么建议?

推荐答案

扩展才是正道。刚刚覆盖的setContentView以正确的方式。 这里的工作示例,但不是抽屉里,我用一个创建一个自定义的TabBar:

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

这是act_layout.xml

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>

这将是你的基地布局包含在act_content帧中的所有其他布局。 接下来,创建一个基类的活动,并执行以下操作:

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

我们做什么,基本上是拦截所有调用的setContentView(INT渣油),夸大我们的布局从XML抽屉,夸大我们的布局的活动(里德在方法调用中提供),将它们结合起来,因为我们需要的,并设置为活动的内容查看

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.

编辑: 当你创建了上面的东西,只是继续写一个应用程序像往常一样,创建布局(没有提到任何一个抽屉)创建活动,但不是简单的伸展活动,延长DrawerActivity,像这样:

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 DraweActivity {

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

会发生什么事,是的setContentView(R.layout.some_layout)被截获。您DrawerActivity加载从XML提供的布局,加载一个非标准的布局为你的抽屉里,结合了他们,然后将其设置为内容查看了该活动。

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.

这篇关于滑动抽屉出现在所有活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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