Android RelativeLayout以编程方式设置"centerInParent"

Ali*_*lin 132 android android-layout android-relativelayout

我有一个像这样的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">

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

我希望能够以编程方式设置positiveButton相同的效果:

android:layout_centerInParent="true"
Run Code Online (Sandbox Code Playgroud)

我怎样才能以编程方式进行此操作?

Reu*_*ton 386

完全未经测试,但这应该工作:

View positiveButton = findViewById(R.id.positiveButton);
RelativeLayout.LayoutParams layoutParams = 
    (RelativeLayout.LayoutParams)positiveButton.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
positiveButton.setLayoutParams(layoutParams);
Run Code Online (Sandbox Code Playgroud)

android:configChanges="orientation|screenSize"在清单中添加您的活动

  • 你可以简单地使用layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT),不需要第二个参数,因为你可以查看[documentation](http://developer.android.com/reference/android/widget/RelativeLayout. LayoutParams.html#addRule(INT)) (27认同)
  • 我想补充一点,这对我也有用,但我不得不改变layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT,0); to layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT,-1); 在我的情况下.您的应用在"锚点"字段中可能需要不同的值. (8认同)
  • 锚字段值可以是**除了0**之外的任何值,表示目前为真.Source的比较如`if(rules [CENTER_IN_PARENT]!= 0 || rules [CENTER_HORIZONTAL]!= 0){`where`0`有效评估为`false` (7认同)
  • 这是有效的,但只有在我执行layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,0)之后; 在父规则的中心之前.谢谢. (4认同)
  • 作为后续问题,是否有人知道此答案中的代码是否可用于动画?例如,将图像从相对左偏移动画到中心位置等. (2认同)

Jua*_*via 13

只是为了从Reuben响应中添加另一种风味,我会像这样使用它来根据条件添加或删除此规则:

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

  • 删除规则的最佳方法是:layoutParams.removeRule(RelativeLayout.CENTER_IN_PARENT); (3认同)

Hir*_*tel 5

我已经做了

1. centerInParent

2. centerHorizo​​ntal

3. centerVertical

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

调用方法:

centerInParent-true

addOrRemoveProperty(mView, RelativeLayout.CENTER_IN_PARENT, true);
Run Code Online (Sandbox Code Playgroud)

centerInParent-假

addOrRemoveProperty(mView, RelativeLayout.CENTER_IN_PARENT, false);
Run Code Online (Sandbox Code Playgroud)

centerHorizo​​ntal-真实

addOrRemoveProperty(mView, RelativeLayout.CENTER_HORIZONTAL, true);
Run Code Online (Sandbox Code Playgroud)

centerHorizo​​ntal-假

addOrRemoveProperty(mView, RelativeLayout.CENTER_HORIZONTAL, false);
Run Code Online (Sandbox Code Playgroud)

centerVertical-正确

addOrRemoveProperty(mView, RelativeLayout.CENTER_VERTICAL, true);
Run Code Online (Sandbox Code Playgroud)

centerVertical-假

addOrRemoveProperty(mView, RelativeLayout.CENTER_VERTICAL, false);
Run Code Online (Sandbox Code Playgroud)

希望这对您有帮助。