Android - DrawerLayout和工具栏在首次打开时不可见

时间:2016-06-07 12:02:55

标签: android drawerlayout navigationview

我目前正在使用工具栏和DrawerLayout开发Android应用程序。主要内容是自定义SurfaceView,我可以在其中绘制不同的形状,文本......左侧菜单是一个NavigationView并用作工具箱(我选择从左侧绘制的内容,然后在SurfaceView上绘制)。一切都工作正常,除了一件事:当我第一次尝试打开左侧菜单(通过单击工具栏或从屏幕左侧滑动)时,项目不可见。虽然我没有点击任何项目(不可见),但它们保持不可见。问题只有在我点击一个不可见的项目后才能解决,之后菜单工作正常。我正在使用自定义主题隐藏状态栏并删除默认操作栏:

<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.NoActionBar"></style>

<style name="ColladiaTheme" parent="AppBaseTheme">

    <!-- Remove action bar -->
    <item name="android:windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>

    <!-- Remove status bar -->
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>

    <!-- Material theme -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

</style>

以下是一些截图:

Menu open but not visible

Menu visible after clicking an item

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.ia04nf28.colladia.DrawColladiaView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/draw_view" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_height="?attr/actionBarSize"
            android:layout_width="?attr/actionBarSize"
            android:minHeight="?attr/actionBarSize"
            android:background="?attr/colorPrimary">
        </android.support.v7.widget.Toolbar>

    </FrameLayout>

    <!-- Left navigation menu -->
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="?attr/actionBarSize"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize"
        android:layout_gravity="start"
        android:fitsSystemWindows="false"
        app:menu="@menu/nav_view_items" >
    </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

这是活动代码:

public class DrawActivity extends AppCompatActivity {
private static final String TAG = "DrawActivity";

private DrawerLayout drawer;
ActionBarDrawerToggle drawerToggle;
private NavigationView nav;
private DrawColladiaView colladiaView;

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

    // Change toolbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    colladiaView = (DrawColladiaView) findViewById(R.id.draw_view);
    colladiaView.setApplicationCtx(getApplicationContext());

    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    // Add burger button
    drawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(drawerToggle);
    // Removes overlay
    drawer.setScrimColor(Color.TRANSPARENT);
    drawer.closeDrawers();

    nav = (NavigationView) findViewById(R.id.nav_view);

    setUpDrawerContent(nav);
}

public void setUpDrawerContent(NavigationView nav)
{
    nav.setNavigationItemSelectedListener(
            new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem item) {
                    selectDrawerItem(item);
                    return true;
                }
            }
    );
}

public void selectDrawerItem(MenuItem item)
{
    switch(item.getItemId())
    {
        case R.id.nav_home:
            Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
            startActivity(intent);
            break;

        default:
            Element newElement = ElementFactory.createElement(getApplicationContext(), item.getTitle().toString());

            if (newElement != null) colladiaView.insertNewElement(newElement);

            drawer.closeDrawers();
            break;
    }
}

@Override
public void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    drawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    drawerToggle.onConfigurationChanged(newConfig);
}

@Override
public void onBackPressed() {
    Log.d(TAG, "onBackPressed");
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

}

1 个答案:

答案 0 :(得分:0)

看起来您应该覆盖onOptionsItemSelected而不是onBackPressed

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    // This will toggle the drawer if android.R.id.home is clicked
    if(drawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    // Handle any other menu item selections...

    return super.onOptionsItemSelected;
}

说实话,我看不到你的抽屉被打开的地方。它应该仍然响应从左边缘的滑动。此代码会将其设置为在单击android.R.id.home时切换(后退/菜单按钮)。