Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative Layout ignoring setMargin()

I'm trying to nest a RelativeLayout inside of a LinearLayout, and give the RelativeLayout a margin that spaces it's edges away from the edges of the LinearLayout. So far I have this:

LayoutInflater.from(context).inflate(R.layout.conversation_view_layout, this, true);

this.setLayoutParams(new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
int tbm = AllConversationsActivity.topAndBottomMargin;
int lrm = AllConversationsActivity.leftAndRightMargin;
((MarginLayoutParams) this.getLayoutParams()).setMargins(lrm, tbm, lrm, tbm);
this.requestLayout();

Which, if I'm reading the API correctly, should set the margin of the RelativeLayout to the numbers specified. It isn't doing so at all, but instead seems to be simply ignoring the setMargins() entirely.

Also, here's my xml

<merge xmlns:android="http://schemas.android.com/apk/res/android">

<ImageView 
    android:id="@+id/conversation_picture"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"/>

<TextView 
    android:id="@+id/conversation_person"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@id/conversation_picture"
    android:singleLine="true"
    android:ellipsize="marquee"/>

<TextView 
    android:id="@+id/conversation_length"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"/>

<TextView 
    android:id="@+id/conversation_first_line"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_below="@id/conversation_picture"
    android:layout_alignParentLeft="true"
    android:singleLine="true"
    android:ellipsize="end"/>

<TextView 
    android:id="@+id/conversation_last_time"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_below="@id/conversation_first_line"
    android:layout_alignParentLeft="true"
    android:singleLine="true"
    android:ellipsize="marquee"/>

I have a small suspicion that these views are forcing the RelativeLayout edges to the parent edges when they make their alignment calls. Does someone know what has to be done to get these margins to set? Or maybe there's a better way to do this entirely? Appreciated.

like image 569
blaineh Avatar asked Nov 17 '11 22:11

blaineh


People also ask

What is layout_ alignparentbottom?

android:layout_alignParentBottomIf true, makes the bottom edge of this view match the bottom edge of the parent. Accommodates bottom margin. May be a boolean value, such as " true " or " false ".


1 Answers

Edit:

You are using MarginLayoutParams instead of LinearLayoutParams. NOTE: if you set LayoutParams on a layout container, you will need to use the parent's type.

So if you have a LinearLayout that contains a RelativeLayout, you need to set LinearLayout.LayoutParams on the RelativeLayout.

// NOT WORKING: LinearLayout.LayoutParams params = (LayoutParams) getLayoutParams();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
int tbm = AllConversationsActivity.topAndBottomMargin;
int lrm = AllConversationsActivity.leftAndRightMargin;
params.setMargins(tbm, lrm, tbm, lrm);
setLayoutParams(params);
like image 110
Entreco Avatar answered Oct 14 '22 00:10

Entreco