如何通过java代码更改android:layout\u alignParentRight运行时
我有一个聊天列表视图,其中发送者的名字应该左对齐,接收者的回复应该在右边,当接收者变成发送者时,反之亦然 这是我的xml文件:如何通过java代码更改android:layout\u alignParentRight运行时,android,android-layout,Android,Android Layout,我有一个聊天列表视图,其中发送者的名字应该左对齐,接收者的回复应该在右边,当接收者变成发送者时,反之亦然 这是我的xml文件: <?xml version="1.0" encoding="utf-8"?> 现在在运行时,我想更改textview的布局\u alignParentRight值。有可能吗?试试: ... RelativeLayout.LayoutParams layoutParams =(RelativeLayout.LayoutParams)yourVi
<?xml version="1.0" encoding="utf-8"?>
现在在运行时,我想更改textview的布局\u alignParentRight值。有可能吗?试试:
...
RelativeLayout.LayoutParams layoutParams =(RelativeLayout.LayoutParams)yourView.getLayoutParams();
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_.....); //ALIGN_PARENT_RIGHT / LEFT etc.
yourView.setLayoutParams(params);
...
是的,您可以使用以下物品:-
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.ALIGN_RIGHT,
textviewid);
从这个代码中你可以得到一些参考 使用此代码
RelativeLayout.LayoutParams _params = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
_params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
apps_layout.setLayoutParams(_params);
谢谢appserv,这对我来说真是个窍门。。应用Align\u parent后,由于layout\u width=“fill\u parent”将其转换为“wrap\u content”后,它无法在一个文本视图上工作,因此它无法工作。。。这里使用的只是常识:)
RelativeLayout.LayoutParams _params = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
_params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
apps_layout.setLayoutParams(_params);