滑动抽屉出现在所有活动中

Zak*_*Zak 16 android menu slidingdrawer

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

我需要的是使滑动抽屉出现在我的所有活动中

我试图创建一个活动,并将其内容视图设置为包含抽屉的xml文件,然后在所有其他活动中扩展该活动,但此解决方案不起作用

有什么建议吗?

Ale*_*lov 41

延伸是正确的方式.只需以正确的方式覆盖setContentView.这是工作示例,但我使用创建的自定义tabbar而不是抽屉:

使用您的抽屉定义布局,如下所示:

这是 act_layout.xml

<LinearLayout
  ...
  android:orientation="vertical"
>
  <YourDrawer
    ...
  />
  <FrameLayout
    android:id="@+id/act_content"
    ...
  >
    // Here will be all activity content placed
  </FrameLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

这将是您的基本布局,以包含act_content框架中的所有其他布局.接下来,创建一个基本活动类,并执行以下操作:

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
    }
}
Run Code Online (Sandbox Code Playgroud)

我们所做的,基本上是拦截对setContentView(int resId)的所有调用,从xml中扩展我们的抽屉布局,为活动扩展我们的布局(通过方法调用中提供的reId),根据需要组合它们,并设置为contentView活动.

编辑: 在你创建了上面的东西后,继续像往常一样编写应用程序,创建布局(没有提到任何抽屉)创建活动,但不是扩展简单的活动,而是扩展DrawerActivity,如下所示:

public abstract class SomeActivity extends DrawerActivity {

    protected void onCreate(Bundle bundle) {
        setContentView(R.layout.some_layout);
    }
}
Run Code Online (Sandbox Code Playgroud)

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


小智 13

最后3年后,这里是对这个重要问题的完整解决方案,奥尔洛夫先生的答案可能没有完全引导他们.

他制作层次结构视图的方法完全没问题,但是有些小错误可能会误导初学者.

  1. 正如Gabe所提到的,你可以在声明中摆脱摘要.
  2. 没有必要用FrameLayout包装两个孩子.父可以是RelativeLayout,LinearLayout等等.但最重要的部分是你必须在Slider之前声明FrameLayout.

所以100%工作的解决方案是这样的:

activity_drawer.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<FrameLayout
    android:id="@+id/drawer_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<YourDrawer
    android:id="@+id/drawer_drawer"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >

</YourDrawer>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

DrawerActivity.java

public class DrawerActivity extends Activity {

protected RelativeLayout fullLayout;
protected FrameLayout frameLayout;

@Override
public void setContentView(int layoutResID) {

    fullLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.activity_drawer, null);
    frameLayout = (FrameLayout) fullLayout.findViewById(R.id.drawer_frame);

    getLayoutInflater().inflate(layoutResID, frameLayout, true);

    super.setContentView(fullLayout);

    //Your drawer content...

}

}
Run Code Online (Sandbox Code Playgroud)

MainActivity.java

public class MainActivity extends DrawerActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

}
Run Code Online (Sandbox Code Playgroud)

不要忘记在清单中声明DrawerActivity.

希望它有所帮助.