简体   繁体   English

为什么这个导航抽屉没有打开?

[英]Why does this Navigation Drawer not open?

I'm trying to make a Navigation Drawer, I'm reading a lot of guides on how to do this and I think it should work now, but the Drawer does not open when I click on the title (when I manually try to open it, then the icon will get smaller, but still no menu appears). 我正在尝试制作导航抽屉,我正在阅读很多关于如何做到这一点的指南,我认为现在应该可以使用,但是当我点击标题时(当我手动尝试打开时)抽屉没有打开它,然后图标会变小,但仍然没有菜单出现)。 Also, I'm not getting any errors or exception, so I think I might be missing something which I cannot figure out myself. 此外,我没有得到任何错误或异常,所以我想我可能会遗漏一些我无法弄明白的事情。 This is the code of my BaseActivity (the drawer activity which gets extended by my MainActivity ): 这是我的BaseActivity的代码(抽屉活动由我的MainActivity扩展):

import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class BaseActivity extends Activity {
    public DrawerLayout drawerLayout;
    public ListView drawerList;
    private String[] drawerListEntries;
    private ActionBarDrawerToggle drawerToggle;

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

        drawerListEntries = getResources().getStringArray(R.array.drawer_items);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_navigation_drawer, R.string.drawer_open, R.string.drawer_close) 
        {
            public void onDrawerClosed(View view) 
            {
                getActionBar().setTitle(R.string.app_name);
            }

            public void onDrawerOpened(View drawerView) 
            {
                getActionBar().setTitle(R.string.hello_world);
            }
        };
        drawerLayout.setDrawerListener(drawerToggle);


        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        drawerList = (ListView) findViewById(R.id.drawer_list);
        drawerList.setAdapter(new ArrayAdapter<String>(getBaseContext(), R.layout.drawer_list_item, drawerListEntries));



        drawerList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int pos, long arg3) {
                String selectedValue = (String) drawerList.getAdapter().getItem(pos);
                Toast.makeText(getBaseContext(), selectedValue, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (drawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);

    }

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

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

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
       boolean drawerOpen = drawerLayout.isDrawerOpen(drawerList);
        menu.findItem(R.id.action_user).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }
}

This is my activity_base.xml : 这是我的activity_base.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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


    <ListView
        android:id="@+id/drawer_list"
        android:layout_width="240dp"
        android:layout_height="wrap_content"
        android:background="@color/light_blue"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:choiceMode="singleChoice"
        android:layout_gravity="start"
        />


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

This is my drawer_list_item.xml : 这是我的drawer_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/drawer_list_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="12dp" 
        android:textSize="24sp" 
        android:textColor="@color/belize_hole"
        android:fontFamily="sans-serif-light">
    </TextView>



</LinearLayout>

And this is my MainActivity.java which extends BaseActivity : 这是我的MainActivity.java ,它扩展了BaseActivity

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class HomeActivity extends BaseActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_home);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.home, menu);
        //return super.onCreateOptionsMenu(menu);
        return true;
    }


}

Anyone who can see the problem? 谁能看到问题?

You are overriding content view from BaseActivity by calling setContentView(R.layout.activity_home); 您通过调用setContentView(R.layout.activity_home);覆盖BaseActivity的内容视图setContentView(R.layout.activity_home); in HomeActivity . HomeActivity

I suggest you to switch fragments instead of activities with Navigation Drawer, it's the best way. 我建议你用Navigation Drawer切换片段而不是活动,这是最好的方法。 So you will have one hosting activity with Navigation Drawer and you'll switch only views inside FrameLayout, for instance. 因此,您将使用导航抽屉进行一次托管活动,并且您将仅切换FrameLayout内的视图。 Take a look here http://developer.android.com/training/implementing-navigation/nav-drawer.html 看看这里http://developer.android.com/training/implementing-navigation/nav-drawer.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM