Android RelativeLayout以编程方式设置;centerInParent“;
我有一个这样的亲戚:Android RelativeLayout以编程方式设置;centerInParent“;,android,android-layout,android-relativelayout,Android,Android Layout,Android Relativelayout,我有一个这样的亲戚: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dip">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip">
<Button
android:id="@+id/negativeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dip"
android:textColor="#ffffff"
android:layout_alignParentLeft="true"
android:background="@drawable/black_menu_button"
android:layout_marginLeft="5dip"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/positiveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dip"
android:textColor="#ffffff"
android:layout_alignParentRight="true"
android:background="@drawable/blue_menu_button"
android:layout_marginRight="5dip"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
如何通过编程实现这一点?完全未经测试,但这应该可以:
添加
android:configChanges=“orientation | screenSize”
在清单中的活动中为了从鲁本响应中添加另一种味道,我使用它来根据条件添加或删除此规则:
RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams) holder.txtGuestName.getLayoutParams();
if (SOMETHING_THAT_WOULD_LIKE_YOU_TO_CHECK) {
// if true center text:
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
holder.txtGuestName.setLayoutParams(layoutParams);
} else {
// if false remove center:
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, 0);
holder.txtGuestName.setLayoutParams(layoutParams);
}
我已经做了1年了。中心家长2。中心水平3。中心垂直,带有true和false
private void addOrRemoveProperty(View view, int property, boolean flag){
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
if(flag){
layoutParams.addRule(property);
}else {
layoutParams.removeRule(property);
}
view.setLayoutParams(layoutParams);
}
如何调用方法:
centerInParent-正确
addOrRemoveProperty(mView, RelativeLayout.CENTER_IN_PARENT, true);
addOrRemoveProperty(mView, RelativeLayout.CENTER_IN_PARENT, false);
addOrRemoveProperty(mView, RelativeLayout.CENTER_HORIZONTAL, true);
addOrRemoveProperty(mView, RelativeLayout.CENTER_HORIZONTAL, false);
addOrRemoveProperty(mView, RelativeLayout.CENTER_VERTICAL, true);
addOrRemoveProperty(mView, RelativeLayout.CENTER_VERTICAL, false);
centerInParent-错误
addOrRemoveProperty(mView, RelativeLayout.CENTER_IN_PARENT, true);
addOrRemoveProperty(mView, RelativeLayout.CENTER_IN_PARENT, false);
addOrRemoveProperty(mView, RelativeLayout.CENTER_HORIZONTAL, true);
addOrRemoveProperty(mView, RelativeLayout.CENTER_HORIZONTAL, false);
addOrRemoveProperty(mView, RelativeLayout.CENTER_VERTICAL, true);
addOrRemoveProperty(mView, RelativeLayout.CENTER_VERTICAL, false);
中心水平-正确
addOrRemoveProperty(mView, RelativeLayout.CENTER_IN_PARENT, true);
addOrRemoveProperty(mView, RelativeLayout.CENTER_IN_PARENT, false);
addOrRemoveProperty(mView, RelativeLayout.CENTER_HORIZONTAL, true);
addOrRemoveProperty(mView, RelativeLayout.CENTER_HORIZONTAL, false);
addOrRemoveProperty(mView, RelativeLayout.CENTER_VERTICAL, true);
addOrRemoveProperty(mView, RelativeLayout.CENTER_VERTICAL, false);
中心水平-错误
addOrRemoveProperty(mView, RelativeLayout.CENTER_IN_PARENT, true);
addOrRemoveProperty(mView, RelativeLayout.CENTER_IN_PARENT, false);
addOrRemoveProperty(mView, RelativeLayout.CENTER_HORIZONTAL, true);
addOrRemoveProperty(mView, RelativeLayout.CENTER_HORIZONTAL, false);
addOrRemoveProperty(mView, RelativeLayout.CENTER_VERTICAL, true);
addOrRemoveProperty(mView, RelativeLayout.CENTER_VERTICAL, false);
中心垂直-正确
addOrRemoveProperty(mView, RelativeLayout.CENTER_IN_PARENT, true);
addOrRemoveProperty(mView, RelativeLayout.CENTER_IN_PARENT, false);
addOrRemoveProperty(mView, RelativeLayout.CENTER_HORIZONTAL, true);
addOrRemoveProperty(mView, RelativeLayout.CENTER_HORIZONTAL, false);
addOrRemoveProperty(mView, RelativeLayout.CENTER_VERTICAL, true);
addOrRemoveProperty(mView, RelativeLayout.CENTER_VERTICAL, false);
中心垂直-错误
addOrRemoveProperty(mView, RelativeLayout.CENTER_IN_PARENT, true);
addOrRemoveProperty(mView, RelativeLayout.CENTER_IN_PARENT, false);
addOrRemoveProperty(mView, RelativeLayout.CENTER_HORIZONTAL, true);
addOrRemoveProperty(mView, RelativeLayout.CENTER_HORIZONTAL, false);
addOrRemoveProperty(mView, RelativeLayout.CENTER_VERTICAL, true);
addOrRemoveProperty(mView, RelativeLayout.CENTER_VERTICAL, false);
希望这能对您有所帮助。有效,但只有在I indertet layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,0)之后才有效;在父规则中居中之前。谢谢。我想补充一点,这也适用于我,但我必须更改layoutParams.addRule(PARENT中的RelativeLayout.CENTER,0);到layoutParams.addRule(父级中的RelativeLayout.CENTER\u,-1);在我的情况下。您的应用程序可能需要“锚定”字段中的其他值。锚定字段值可以是0以外的任何值,以表示当前为真。源代码具有类似于
if的比较(规则[CENTER_IN_PARENT]!=0 | |规则[CENTER_HORIZONTAL]!=0){
其中0
有效地计算为false
,作为后续问题,是否有人知道此答案中的代码是否可用于动画?例如,从相对左偏移到中心位置等动画图像。您可以简单地使用layoutParams.addRule(RelativeLayout.CENTER\u in\u PARENT),不需要第二个参数,因为您可以选择删除规则的最佳方法:layoutParams.RemoveUle(RelativeLayout.CENTER\u in\u PARENT);