Unable to see navigation drawer

2019-09-10 05:51发布

I have gone through similar questions and their solutions here, but I can't get past my issue after spending few hours on it. I looked at many solutions, tried them, but still can't get to see the navigation drawer on my screen. I will really appreciate if someone could tell me what am I missing here.

Thanks in advance.

Here's my xml :

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="400dp"
    android:layout_height="400dp"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            tools:context=".Menu">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:paddingLeft="16dp"
                android:paddingRight="16dp"
                android:textColor="#fff"
                android:text="test"
                android:id="@+id/text" />

        </RelativeLayout>

        <ListView
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            android:choiceMode="singleChoice"
            android:background="#fff"
            android:id="@+id/left_drawer" >
        </ListView>

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

Here's my Menu.java :

>public class Menu extends AppCompatActivity {
>
>    public DrawerLayout dlayout;
>    public ListView flist;
>    public String[] menu;
>    //public ArrayAdapter<String> mAdapter;
>
>    @Override
>    protected void onCreate(Bundle savedInstanceState) {
>        super.onCreate(savedInstanceState);
>        setContentView(R.layout.activity_menu);
>
>        menu = getResources().getStringArray(R.array.nav_drawer_items);
>        dlayout = (DrawerLayout) findViewById(R.id.drawer_layout);
>        flist = (ListView) findViewById(R.id.left_drawer);
>
>        flist.setAdapter(new ArrayAdapter<String>(this, R.layout.activity_menu, >R.id.text, menu));
>    }
>}

I don't see any output.Screen output

3条回答
迷人小祖宗
2楼-- · 2019-09-10 06:43
<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">

<include
    android:id="@+id/header"
    layout="@layout/menu"/>

<FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:id="@+id/frame"
        android:textSize="24sp"
        android:gravity="center"
        android:layout_marginTop="50dp"/>

<ListView
    android:id="@+id/list_item"
    android:layout_width="200dp"
    android:layout_height="match_parent"
    android:dividerHeight="1dp"
    android:layout_gravity="left|start"
    android:background="#ffeeeeee"/></android.support.v4.widget.DrawerLayout>

MainActivity.java

public class MainActivity extends Activity {

String[] names = {"android","java","spring","html"};
ListView list;
FrameLayout frame;
ActionBarDrawerToggle action;
DrawerLayout drawer;
Button but;


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

    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    frame = (FrameLayout) findViewById(R.id.frame);
    list = (ListView) findViewById(R.id.list_item);
    but = (Button) findViewById(R.id.menu);

    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, names);
    list.setAdapter(adapter);

    action = new ActionBarDrawerToggle(this, drawer, R.string.drawer_open, R.string.drawer_close) {

        /* Called when drawer is closed */
        public void onDrawerClosed(View view) {
            //Put your code here
        }

        /* Called when a drawer is opened */
        public void onDrawerOpened(View drawerView) {
            //Put your code here
        }
    };
    drawer.setDrawerListener(action);

    but.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (drawer != null) {
                if (drawer.isDrawerOpen(list)) {
                    drawer.closeDrawer(list);
                } else {
                    drawer.openDrawer(list);
                    process();
                }
            }
        }
    });


}

private void process() {

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            FragmentTransaction ft=getFragmentManager().beginTransaction();

            switch (names[position])
            {
                case "android":
                {
                   yourClass obj=new yourClass();
                   //ft.replace(R.id.frame,obj);
                   break
                }
                case "java":
                {
                   yourClass obj=new yourClass();
                   //ft.replace(R.id.frame,obj);
                    break;
                }
                case "spring":
                {
                     yourClass obj=new yourClass();
                    //ft.replace(R.id.frame,obj);
                    break;
                }
                case "html":
                {
                   yourClass obj=new yourClass();
                   //ft.replace(R.id.frame,obj);
                    break;
                }

                default:
                {
                    Toast.makeText(MainActivity.this,"you have to click another one",Toast.LENGTH_LONG).show();
                }
            }
            ft.commitAllowingStateLoss();
            list.setItemChecked(position, true);
            drawer.closeDrawer(list);
        }

    });
}

}

menu.xml

<LinearLayout 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:layout_width="match_parent"
tools:context=".MainActivity"
android:layout_height="wrap_content">

<Button
    android:id="@+id/menu"
    android:title="menu"
    android:icon="@drawable/menu"
    android:background="@drawable/menu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:backgroundTint="#0a0a0a" /></LinearLayout>
查看更多
闹够了就滚
3楼-- · 2019-09-10 06:49

You need to toggle the open/close of the drawer. To do so, you can do the following :

1 - Implement the toggleDrawer method

public void toggleDrawer(boolean shouldBeVisible){
    if(shouldBeVisible){
        dlayout.openDrawer(dlayout);
    } else {
        dlayout.closeDrawers();
    }
}

2 - Call it on onCreate (note : this is just for testing)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    toggleDrawer(View.VISIBLE);
}

3 - Like Daryl said, implement a button so you can actively toggle (open and close the drawer to your liking)

myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v){
        if(dlayout.getVisiblity() == View.VISIBLE) {
            // drawer is visible -> close
            toggleDrawer(false);
        } else {
            // drawer is gone/invisible -> open
            toggleDrawer(true);
        }
    }
})

4 - (Make sure your DrawerLayout contains only 2 child views)

To add a navigation drawer, declare your user interface with a DrawerLayout object as the root view of your layout. Inside the DrawerLayout, add one view that contains the main content for the screen (your primary layout when the drawer is hidden) and another view that contains the contents of the navigation drawer.

Note : Forgot to add this but is indeed an invaluable reference (thanks to @Meet, upvoted)

查看更多
孤傲高冷的网名
4楼-- · 2019-09-10 06:54

seems that there should be problem in your layout file.

Remember that DrawerLayout allows maximum 2 child views. Have a look at this Google Doc.

It should be like:

<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">
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
查看更多
登录 后发表回答