Eric98118
Eric98118

Reputation: 409

How do I get space between consecutive RelativeLayouts inside a LinearLayout?

I'm trying to create a list of clickable image buttons w/ text that fit inside a HorizontalScrollView. The images/content will be set programmatically. The best way to do this seemed to be a LinearLayout, that then contained a series of RelativeLayouts that contained the views to display the relevant content. However, I'm having trouble getting space between each RelativeLayout. Even though I've set margins in xml and programmatically they seem to be ignored and the RelativeLayout objects are squished together.

Some code:

<RelativeLayout
    android:id="@+id/details_image_button"
    android:layout_width="75dp"
    android:layout_height="100dp"
    android:layout_marginLeft="10dp"
    android:background="#00ff78">

    <ImageView
        android:id="@+id/loadable_image_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
         />

    <TextView
        android:id="@+id/details_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#b083ef"
        android:text="PH - Info about title"
        android:layout_alignParentBottom="true"
        />

//Code below is looped through several times
        RelativeLayout imageButtonLayout = (RelativeLayout) inflater.inflate(R.layout.details_image_button, null);
        RelativeLayout.LayoutParams imageButtonLayoutParams = new RelativeLayout.LayoutParams(100, 100);
        imageButtonLayoutParams.setMargins(10, 10, 10, 10);
        imageButtonLayout.setLayoutParams(imageButtonLayoutParams);

The current result that I am getting is a solid green (background color of the RelativeLayout) rather than the expected result of a group of RelativeLayouts with a space between each. How can I best get a margin or buffer between each RelativeLayout?

Upvotes: 0

Views: 254

Answers (1)

Vikram
Vikram

Reputation: 51571

If your RelativeLayout is inside a LinearLayout, the LayoutParams you need to use would be LinearLayout.LayoutParams:

RelativeLayout imageButtonLayout = (RelativeLayout) 
                                   inflater.inflate(R.layout.details_image_button, null);
    LinearLayout.LayoutParams imageButtonLayoutParams = new 
                                   LinearLayout.LayoutParams(100, 100);
    imageButtonLayoutParams.setMargins(10, 10, 10, 10);
    imageButtonLayout.setLayoutParams(imageButtonLayoutParams);

LayoutParams come from the parent, not the child.

Upvotes: 3

Related Questions