相对布局,忽略setMargin() [英] Relative Layout ignoring setMargin()

查看:91
本文介绍了相对布局,忽略setMargin()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将RelativeLayout嵌套在LinearLayout内,并给RelativeLayout留出一定的余量,以使其边缘与LinearLayout的边缘间隔开.到目前为止,我有这个:

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();

如果我正确阅读了API,则应将RelativeLayout的边距设置为指定的数字.它根本没有这样做,但是似乎只是完全忽略了setMargins().

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.

另外,这是我的xml

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"/>

我有点怀疑这些视图在进行对齐调用时会将RelativeLayout边缘强制为父边缘. 有人知道要设置这些边距需要做什么吗?还是有一种更好的方法可以完全做到这一点? 赞赏.

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.

推荐答案

您正在使用MarginLayoutParams而不是LinearLayoutParams. 注意:如果在布局容器上设置LayoutParams,则将需要使用父级的类型.

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.

因此,如果您的LinearLayout包含RelativeLayout,则需要在RelativeLayout上设置LinearLayout.LayoutParams.

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);

这篇关于相对布局,忽略setMargin()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆