导航抽屉未显示且未引发错误

时间:2016-05-09 07:08:07

标签: android navigation-drawer

以下代码是BaseActivity,此活动用于实现不同活动的导航抽屉

public class BaseActivity extends AppCompatActivity implements
    NavigationView.OnNavigationItemSelectedListener {

private NavigationView navigationView;
private DrawerLayout fullLayout;
private Toolbar toolbar;
private ActionBarDrawerToggle drawerToggle;
private int selectedNavItemId;

@Override
public void setContentView(@LayoutRes int layoutResID) {

    fullLayout = (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_base, null);

    FrameLayout activityContainer = (FrameLayout) fullLayout.findViewById(R.id.activity_content);
    getLayoutInflater().inflate(layoutResID, activityContainer, true);

    /**
     * Note that we don't pass the child's layoutId to the parent,
     * instead we pass it our inflated layout.
     */
    super.setContentView(fullLayout);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    navigationView = (NavigationView) findViewById(R.id.navigationView);

    if (useToolbar())
    {
        setSupportActionBar(toolbar);
    }
    else
    {
        toolbar.setVisibility(View.GONE);
    }

    setUpNavView();
}
protected boolean useToolbar()
{
    return true;
}

protected void setUpNavView()
{
    navigationView.setNavigationItemSelectedListener(this);

    if( useDrawerToggle()) { // use the hamburger menu
        drawerToggle = new ActionBarDrawerToggle(this, fullLayout, toolbar,
                R.string.nav_drawer_opened,
                R.string.nav_drawer_closed);

        fullLayout.setDrawerListener(drawerToggle);
        drawerToggle.syncState();
    } else if(useToolbar() && getSupportActionBar() != null) {
        // Use home/back button instead
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(getResources()
                .getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha));
    }
}

protected boolean useDrawerToggle()
{
    return true;
}


@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
    fullLayout.closeDrawer(GravityCompat.START);
    selectedNavItemId = menuItem.getItemId();

    return onOptionsItemSelected(menuItem);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    switch (id)
    {
        case R.id.action_main:
            startActivity(new Intent(this, MainActivity.class));
            return true;

        case R.id.action_order:
            startActivity(new Intent(this, Orders.class));
            return true;

        case R.id.rate_card:
            startActivity(new Intent(this,RateCard.class));
            return true;
    }

    return super.onOptionsItemSelected(item);
}

这是用于实现目的的xml文件设计

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    android:id="@+id/activity_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:fitsSystemWindows="true">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            android:background="?attr/colorPrimary"
            app:theme="@style/MainActionBar"/>
        <FrameLayout
            android:id="@+id/activity_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <android.support.design.widget.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="wrap_content"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/menu_base"/>
</android.support.v4.widget.DrawerLayout>

我尝试了所有的事情,但没有得到正确的实施方式。任何建议如何纠正这将是有帮助的

0 个答案:

没有答案