royB
royB

Reputation: 12977

RelativeLayout margin not working inside listView

I'm wondering why what i'm trying to accomplish isn't working

Relative layout code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#649760" 
            android:layout_marginRight="30dp">

    <ImageView
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:background="#000000"/>

</RelativeLayout>

How it looks on android studio preview:

Android studio preview. notice the white after the green

notice the white right to the green.

I've also added a list_view.xml in order to remove the divider and control the background color

 <?xml version="1.0" encoding="utf-8"?>
 <ListView xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:dividerHeight="0dp"
      android:divider="@null"
      android:background="#ffffff">
 </ListView>

but what i'm getting (screen capture from my mobile) is that the green captures all the remaining width.

btw i've solved it by giving a padding to list_view.xml but i cannot use such a solution because i have multiple views in the list and not all of them should get this kind of margin

Any help will be greatly appreciated, and will be rewarded with thumbs up (-:

Screen capture, no white

Upvotes: 0

Views: 2067

Answers (1)

Andros
Andros

Reputation: 4069

The rows of a ListView use AbsListView.LayoutParams, when you check out the doc : http://developer.android.com/reference/android/widget/AbsListView.LayoutParams.html

you can notice that they aren't any margin available.

So the workaround is to add another View on top of your cells, this should work :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#649760"
            android:layout_marginRight="30dp">

        <ImageView
                android:layout_width="200dp"
                android:layout_height="50dp"
                android:background="#000000"/>

    </RelativeLayout>
</LinearLayout>

An other workaround, add a View at the right of your "rootView" :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#649760">

    <View
            android:layout_width="30dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:background="@color/white"/>

    <ImageView
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:background="#000000"/>

</RelativeLayout>

I know it's not the best for performance, but it's the only workaround I found.

Upvotes: 3

Related Questions