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

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

问题描述

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

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 即可.这是工作示例,但我没有使用抽屉,而是使用创建的自定义标签栏:

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

<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 resId) 的调用,从 xml 扩展我们的抽屉布局,扩展我们的活动布局(通过方法调用中提供的 reId),根据需要组合它们,然后设置为活动的内容视图.

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

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

发生了什么,是 setContentView(R.layout.some_layout) 被拦截了.您的 DrawerActivity 加载您从 xml 提供的布局,为您的抽屉加载一个标准布局,将它们组合起来,然后将其设置为 Activity 的 contentView.

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天全站免登陆