简体   繁体   中英

DrawerLayout background for ListView

I have the following for my DrawerLayout :

<?xml version="1.0" encoding="utf-8"?>

<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"
    android:background="@drawable/background_main">

    <!-- Framelayout to display Fragments -->
    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- Listview to display slider menu -->
    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@drawable/background"
        android:choiceMode="singleChoice"
        android:divider="@color/transparent"
        android:dividerHeight="0dp" />

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

I want to have a background image for my ListView with a black view on top of it that has some opacity. For my iPhone app it's very easy--I just have a UIImageView with a UIView on top that has a black background and opacity of .4 and everything works.

Is there a way to do this in Android? Also, this image will be downloaded from the server...so I need to fit that constraint as well.

EDIT

I tried as per the first answer below and got the below error:

Process: com.mounzaapp, PID: 17534
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mounzaapp/com.mounzaapp.views.dashboard.DashboardFragmentActivity}: java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
        at android.support.v4.widget.DrawerLayout.isDrawerView(DrawerLayout.java:857)
        at android.support.v4.widget.DrawerLayout.closeDrawer(DrawerLayout.java:1058)
        at com.mounzaapp.views.dashboard.DashboardFragmentActivity.onItemClick(DashboardFragmentActivity.java:257)
        at com.mounzaapp.views.dashboard.DashboardFragmentActivity.onCreate(DashboardFragmentActivity.java:148)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)

I get the error when doing: drawerLayout.closeDrawer(listLayout); . listLayout is the RelativeLayout that contains the ListView .

EDIT 2

Turns out all I had to do was delete the app from my android device and then rebuild. Everything else worked!

You can only put two child views inside a DrawerLayout , but you can put the ListView inside a RelativeLayout . Like this:

<?xml version="1.0" encoding="utf-8"?>

<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"
    android:background="@drawable/background_main">

    <!-- Framelayout to display Fragments -->
    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- RelativeLayout to display slider menu -->
    <RelativeLayout
        android:id="@+id/drawer_container"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@drawable/background" >

        <ListView
            android:id="@+id/list_slidermenu"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/background"
            android:choiceMode="singleChoice"
            android:divider="@color/transparent"
            android:dividerHeight="0dp" />

        <!-- Whatever view you want inside the RelativeLayout -->

    </RelativeLayout>

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

**** EDIT ****

Only two child view in DrawerLayout reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM