Android RelativeLayout以编程方式设置“ centerInParent”


139

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

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

android:layout_centerInParent="true"

如何以编程方式进行此设置?

Answers:


401

完全未经测试,但这应该可以:

View positiveButton = findViewById(R.id.positiveButton);
RelativeLayout.LayoutParams layoutParams = 
    (RelativeLayout.LayoutParams)positiveButton.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
positiveButton.setLayoutParams(layoutParams);

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


5
那行得通,但只有在我了解了layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,0);之后,在父规则中居中。谢谢。
Alin

9
我想补充一点,这也对我有用,但是我不得不更改layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT,0); 到layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT,-1); 在我的情况下。您的应用可能在“锚定”字段中要求使用其他值。
本·麦克

7
锚字段值可以是0以外的任何值,以表示当前为true。来源具有比较结果,例如有效评估的if (rules[CENTER_IN_PARENT] != 0 || rules[CENTER_HORIZONTAL] != 0) {地方0false
Dori 2012年

2
作为后续问题,是否有人知道此答案中的代码是否可以在动画中使用?像例如从动画的相对的图像向左偏移的中心位置等
强尼

27
您只需使用layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT),就不需要第二个参数,因为您可以签入文档
tarmelop 2013年

14

只是为了从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);
    }

3
删除规则的最佳方法是:layoutParams.removeRule(RelativeLayout。CENTER_IN_PARENT);
Zahid Rasheed

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);
}

调用方法:

centerInParent-true

addOrRemoveProperty(mView, RelativeLayout.CENTER_IN_PARENT, true);

centerInParent-假

addOrRemoveProperty(mView, RelativeLayout.CENTER_IN_PARENT, false);

centerHorizo​​ntal-真实

addOrRemoveProperty(mView, RelativeLayout.CENTER_HORIZONTAL, true);

centerHorizo​​ntal-假

addOrRemoveProperty(mView, RelativeLayout.CENTER_HORIZONTAL, false);

centerVertical-正确

addOrRemoveProperty(mView, RelativeLayout.CENTER_VERTICAL, true);

centerVertical-假

addOrRemoveProperty(mView, RelativeLayout.CENTER_VERTICAL, false);

希望这对您有帮助。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.