相对布局忽略setMargin()

bla*_*neh 18 android android-widget android-layout

我试图在一个RelativeLayout内部嵌套LinearLayout,并给它RelativeLayout一个边缘,使它的边缘远离边缘LinearLayout.到目前为止我有这个:

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();
Run Code Online (Sandbox Code Playgroud)

其中,如果我正确读取API,应将RelativeLayout的边距设置为指定的数字.它根本不是这样做的,而是setMargins()完全忽略了它.

另外,这是我的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"/>
Run Code Online (Sandbox Code Playgroud)

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

Ent*_*eco 34

编辑:

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

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

// 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);
Run Code Online (Sandbox Code Playgroud)

  • Entreco,你原来是完全正确的,这是因为我创建了错误的params所有者,但是你实施的实现有点错误.这是最终工作的代码:`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); requestLayout();`谢谢你的帮助! (2认同)