Memo36
Memo36

Reputation: 333

Use an Image for DrawerLayout background

I've search on line and here, but I can not find any usefull answer on : "How can I set an Image as a background for my NavigationDrawer's ListView ?"

The problem is whenever my ListView's background isn't a "solid" color (#aabbcc for example), the DrawerLayout doesn't show.

Here's my layout :

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

        <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="wrap_content"
            android:layout_height="match_parent"
            android:background="#aabbcc"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
             />        
    [... Rest of main layout ...]

If I change the android:background="#aabbcc" to android:background="@drawable/menu_bg", my app screen just get grey and the Drawer doesn't show.

Solutions I(ve try but doesn't work (same result) : DrawerLayout background for ListView Drawer layout has overridden the background

Is it possible to set an Image instead of a single color in a NavigationDrawer ?

Thanks.

Upvotes: 0

Views: 4327

Answers (1)

Simas
Simas

Reputation: 44118

What about making the drawer a RelativeLayout and adding an ImageView underneath the ListView:

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

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- Your main layout -->
</FrameLayout>

<RelativeLayout
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:layout_gravity="start">
    <!-- The navigation drawer -->
    <ImageView
        android:id="@+id/drawer_bg"
        android:src="@drawable/img2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <ListView
        android:id="@+id/drawer_list"
        android:background="@null"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

Upvotes: 2

Related Questions