如何通过java代码更改android:layout_alignParentRight运行时

Zoo*_*bie 4 android android-layout

我有聊天列表视图,其中发送者名称应该左对齐,接收者的回复应该在右侧,然后当接收者成为发送者时反之亦然.

这是我的xml文件:

<?xml version="1.0" encoding="utf-8"?>
Run Code Online (Sandbox Code Playgroud)

<TextView android:id="@+id/chatmessagename"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true"
    android:textSize="16sp" android:textStyle="bold"
    android:paddingBottom="1dp" />
<TextView android:id="@+id/chatmessagedate"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"    
    android:autoLink="none" />
<TextView android:id="@+id/chatmessagetext"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@id/chatmessagename"
    android:autoLink="all" />
Run Code Online (Sandbox Code Playgroud)

现在在运行时我想更改textview的layout_alignParentRight值.有可能吗?

Vya*_*kin 13

尝试:

... 
RelativeLayout.LayoutParams layoutParams =(RelativeLayout.LayoutParams)yourView.getLayoutParams();
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_.....); //ALIGN_PARENT_RIGHT / LEFT etc.
yourView.setLayoutParams(params);
... 
Run Code Online (Sandbox Code Playgroud)