Navigation Drawer without Actionbar

2019-02-08 10:03发布

问题:

I searched many sites (stackoverflow as well) but couldn't understand how to implement the navigation drawer without the action bar. I know that this question has already been asked here but it doesn't have a proper explanation and code. I am a beginner in android development so can anyone please explain me with code How to make a navigation drawer without an action bar

Thanks in advance!

回答1:

There is no relation between navigation drawer and action bar. Try this code, it will help you:

<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"
tools:context=".MainActivity" >

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

<ListView
    android:id="@+id/drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#FFF"
    android:choiceMode="singleChoice"/>

Java code is here:

public class MainActivity extends Activity {
final String[] data ={"one","two","three"};

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

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);

     final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
     final ListView navList = (ListView) findViewById(R.id.drawer);
     navList.setAdapter(adapter);
     navList.setOnItemClickListener(new OnItemClickListener(){
             @Override
             public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
                     drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
                             @Override
                             public void onDrawerClosed(View drawerView){
                                     super.onDrawerClosed(drawerView);

                             }
                     });
                     drawer.closeDrawer(navList);
             }
     });
    }
}


回答2:

Just add your DrawerLayout like @mohan did, then if you have a button or something you tap on and want to open the drawer, just do like this :

drawer.openDrawer(Gravity.LEFT);

and to close :

drawer.closeDrawer(Gravity.LEFT);


回答3:

Starting from the Sample Android NavigationDrawer app:

  1. Use the Activity theme: Theme.NoTitleBar
  2. Erase all the ActionBar references in the code
  3. Create a button and in the clickListener call:

    drawer.openDrawer(Gravity.LEFT);