Using Navigation Drawer without TitleBar or ActionBar

前端 未结 3 1183
难免孤独
难免孤独 2020-12-16 02:11

I\'m adding a Navigation Drawer to this app that I am developing and I have scoured the internet; forums, stackoverflow, android developer documentation, and still have not

相关标签:
3条回答
  • 2020-12-16 02:52

    It's quite simple actually. Easier than with ActionBar. I'm writing the answer with almost simplest of layouts

    make your xml something like this:

    <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" >
    
    <!-- This is how your main page will look, just 2 buttons -->
    
        <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="100dp" >
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:onClick="onLeft"
                android:text="left" />
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:onClick="onRight"
                android:text="right" />
        </RelativeLayout>
    
    <!-- Left Drawer -->
        <RelativeLayout
        android:id="@+id/whatYouWantInLeftDrawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start" >
    
            <ListView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/background_dark" />
        <!-- you can have many more widgets here like buttons or labels -->
        </RelativeLayout>
    
    
    <!-- Right Drawer -->
        <RelativeLayout
        android:id="@+id/whatYouWantInRightDrawer"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right" >
    
            <ListView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/holo_green_light" />
        <!-- you can have many more widgets here like buttons or labels -->
        </RelativeLayout>
    
    </android.support.v4.widget.DrawerLayout>
    

    Then make your activity something like this:

    public class MainActivity extends Activity {
    
    RelativeLayout leftRL;
    RelativeLayout rightRL;
    DrawerLayout drawerLayout;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    //      I'm removing the ActionBar.
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.activity_main);
    
            leftRL = (RelativeLayout)findViewById(R.id.whatYouWantInLeftDrawer);
            rightRL = (RelativeLayout)findViewById(R.id.whatYouWantInRightDrawer);
            drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        }
    
        public  void onLeft(View view) {
             drawerLayout.openDrawer(leftRL);
        }
    
        public  void onRight(View view) {
             drawerLayout.openDrawer(rightRL);
        }
    }
    

    That's it. Hope it helps.

    0 讨论(0)
  • 2020-12-16 02:54

    I was trying to add navigation drawer to an already existing activity (Which was without action bar) solution for me was to remove the line:

    android:fitsSystemWindows="true" 
    

    from <android.support.v4.widget.DrawerLayout in my Activity's xml file.

    0 讨论(0)
  • 2020-12-16 03:09

    I know that it is possible to do this without using either of these things. What I am wondering is how.

    Step #1: Follow the instructions for using DrawerLayout, such as the steps in this training guide, skipping anything related to the action bar.

    Step #2: There is no step #2.

    While DrawerLayout can work with the action bar, it is not required, and actually requires additional setup.

    0 讨论(0)
提交回复
热议问题