Drawer layout with fixed menu item

2019-05-26 21:15发布

问题:

We have implemented DrawerLayout which is working fine. However we would like a fixed menu option(logout) that has to be at the bottom of the screen and show up when the drawer opens only. The footer was not an option as it would always shows up as last item in the menu list rather we always want it at the bottom of the menu. We have been able to have a relative layout with the button that is made visible in the onDrawerOpened() but opening of the draw closes that button as the drawer becomes the topmost layer. Requesting focus does not help as the drawer open animation happens even after focus reqest

In any case, we are looking for:

  1. Either how to add this menu item always at the bottom or
  2. Make the menu show up after the drawer opens.

        public void onDrawerOpened(View drawerView) {
    
            getActionBar().setTitle(getTitle());             
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu();             
            logoutButton.setVisibility(View.VISIBLE);
            logoutButton.setFocusable(true);
            logoutButton.requestFocus();
            logoutButton.requestLayout();
            //drawerLayout.addView(logoutView, 0);
        }
    

回答1:

You attempt with the RelativeLayout was giving unexpected result since it wasnt inside the content frame. Here is what I did:

<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" />
<RelativeLayout
    android:id="@+id/relative_layout"
   android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start" >

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:background="#111"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />

   <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

  <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/activatedBackgroundIndicator"
        android:layout_alignParentBottom="true"
        android:id="@+id/holder" >

        <TextView

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="?android:attr/activatedBackgroundIndicator"
            android:gravity="center_vertical"
            android:id="@+id/logout_item"
            android:minHeight="?android:attr/listPreferredItemHeightSmall"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:text="@string/logout"
            android:textAppearance="?android:attr/textAppearanceListItemSmall"
            android:textColor="#fff" />

    </FrameLayout>
    </RelativeLayout>

</RelativeLayout>

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

Don't forget to run HierarchyViewer to get rid of unnecessary ViewGroup.