使用自定义

时间:2016-01-14 06:07:09

标签: android navigation-drawer

我想在双面创建抽屉。以下是图片: -

Left drawer

Right drawer

我已成功创建左抽屉。 以下是代码:

private FragmentDrawer drawerFragment;
private DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawerFragment = (FragmentDrawer) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
    drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
    drawerFragment.setDrawerListener(this);
    displayView(0);


}

@Override
public void onDrawerItemSelected(View view, int position) {
    displayView(position);
}

private void displayView(int position) {
    Fragment fragment = null;
    String title = getString(R.string.app_name);
    switch (position) {
        case 0:
            fragment = new HomeFragment();
            title = getString(R.string.title_dashboard);
            break;

        case 8:

            AppSharedPreference.getInstance(HomeActivity.this).setLogin(false);
            startActivity(new Intent(HomeActivity.this, LoginIcoachActivity.class));
            finish();
            break;

    }

    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.container_body, fragment);
        fragmentTransaction.commit();

        // set the toolbar title
        getSupportActionBar().setTitle(title);
    }
}

}

XML文件: -

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/container_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />
    </LinearLayout>

    <include android:id="@+id/home"
        layout="@layout/content_home"/>

</LinearLayout>

<fragment
    android:id="@+id/fragment_navigation_drawer"
    android:name="fragment.FragmentDrawer"
    android:layout_width="@dimen/nav_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:layout="@layout/fragment_navigation_drawer"
    tools:layout="@layout/fragment_navigation_drawer" />

我已按照以下链接: -

android hive

请帮助我实现这一目标。

2 个答案:

答案 0 :(得分:2)

您可以使用与第一个抽屉相同的方式添加第二个抽屉,只需使用False

答案 1 :(得分:0)

在XML中尝试以下代码:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/container_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />
    </LinearLayout>

    <include android:id="@+id/home"
        layout="@layout/content_home"/>

</LinearLayout>

<fragment
    android:id="@+id/fragment_navigation_drawer"
    android:name="fragment.FragmentDrawer"
    android:layout_width="@dimen/nav_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:layout="@layout/fragment_navigation_drawer"
    tools:layout="@layout/fragment_navigation_drawer" />

<fragment
    android:id="@+id/fragment_navigation_drawer_right"
    android:name="fragment.FragmentDrawer" // some other fragment
    android:layout_width="@dimen/nav_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    app:layout="@layout/fragment_navigation_drawer"
    tools:layout="@layout/fragment_navigation_drawer" />

在您的活动中,使用fragment_navigation_drawer_right id。

获取片段实例