你可以。
DrawerLayout是一个布局,其中包含2个布局 - 1是菜单,另一个包含您的内容。
这意味着您可以在菜单抽屉中放置一个片段,并将其填入任何您想要的内容。
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/activityFrame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<FrameLayout
android:id="@+id/drawer"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@drawable/backgound_menu" >
<fragment
android:id="@+id/menuFragment"
android:name="com.foo.bar.fragment.MenuFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/menu_fragment" />
</FrameLayout>
您可以通过一个基类的活动,您的菜单活动扩展做到这一点。然后在基本活动中添加一个setFrameContent方法,该方法将使用内容片段填充活动框架。在你的子类中,在onCreate方法中调用setFrameContent而不是setConentView。
基本活动
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_fragment_drawer);
}
public void setFrameContent(int activityLayout) {
mContent.addView(
getLayoutInflater().inflate(
activityLayout,
mContent, false),
new LinearLayout.LayoutParams(DrawerLayout.LayoutParams.MATCH_PARENT,
DrawerLayout.LayoutParams.MATCH_PARENT));
}
子类
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_fragment_content);
}
从那里,你可以自由地做任何与MenuFragment。
你能举一个完整的例子@Phil H –