如何以编程方式设置相对布局中按钮的layout_align_parent_right属性?

302

我有一个相对布局,我正在以编程方式创建:

 RelativeLayout layout = new RelativeLayout( this );
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT);

现在我有两个按钮,我想将它们添加到这个相对布局中。但问题是,这两个按钮都显示在相对布局的左侧,重叠在彼此上面。

buttonContainer.addView(btn1);
buttonContainer.addView(btn2);

现在我想知道如何以编程方式设置按钮的android:layout_alignParentRight="true"android:layout_toLeftOf="@id/btn"属性,就像在xml中所做的那样。

5个回答

667
你可以使用View.getLayoutParams从代码中访问任何LayoutParams。你必须非常清楚自己正在访问的LayoutParams。通常通过检查包含的ViewGroup是否具有LayoutParams内部子项来实现此目的,然后选择相应的参数。在这个例子中,它是RelativeLayout.LayoutParams。你将使用RelativeLayout.LayoutParams#addRule(int verb)RelativeLayout.LayoutParams#addRule(int verb,int anchor)

你可以通过代码获取它:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);

button.setLayoutParams(params); //causes layout update

1
如果锚点没有 id,该怎么办? params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of); TextView tvUserName = new TextView(act); - Saint Robson
19
添加ID怎么样? - user2161301
1
如果buttonRecycleView中怎么办?将相同或相反的规则添加到同一部件不是问题吗? - János
它只能工作一次,如果我更改对齐方式,它就无法更新。请问可能是什么问题? - Shshank Bhong
1
我尝试使用它,但在我的代码中的addRule处变成了红色? - user10147255
抱歉我点了反对票,那是一个意外,我两周后才发现... - Bungler

15

若要添加一个值为true或false的RelativeLayout属性,请使用0表示false,RelativeLayout.TRUE表示true:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) button.getLayoutParams()
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE)

无论属性是否已添加,您仍需使用 addRule(verb, subject) 来启用/禁用它。然而,在API 17之后,您可以使用 removeRule(verb),它只是 addRule(verb, 0) 的一种快捷方式。


13
  1. 你需要为需要引用的按钮创建一个ID: btn1.setId(1);
  2. 你可以使用params变量向布局添加参数,我认为方法是addRule(),请查看Android Java文档中的LayoutParams对象。

我非常感激任何一段代码。我在按钮中找不到任何方法。 - Fahad Ali
设置id并不能解决问题,因为它需要一个真正的资源id而不仅仅是一个数字,因为setId被注释为@IdRes。 - Kevin Tan
那倒是新的,过去你可以从布局中获取资源编号,并将其用作不会冲突的资源ID的基础。我猜Google一定已经阻止了这种做法。 - codeScriber

2

Kotlin版本:

使用这些扩展与中缀函数一起,可以简化后续调用。

infix fun View.below(view: View) {
    (this.layoutParams as? RelativeLayout.LayoutParams)?.addRule(RelativeLayout.BELOW, view.id)
}

infix fun View.leftOf(view: View) {
    (this.layoutParams as? RelativeLayout.LayoutParams)?.addRule(RelativeLayout.LEFT_OF, view.id)
}

infix fun View.alightParentRightIs(aligned: Boolean) {
    val layoutParams = this.layoutParams as? RelativeLayout.LayoutParams
    if (aligned) {
        (this.layoutParams as? RelativeLayout.LayoutParams)?.addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
    } else {
        (this.layoutParams as? RelativeLayout.LayoutParams)?.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0)
    }
    this.layoutParams = layoutParams
}

然后将它们用作中缀函数调用:
view1 below view2
view1 leftOf view2
view1 alightParentRightIs true

或者您可以像使用普通函数一样使用它们:

view1.below(view2)
view1.leftOf(view2)
view1.alightParentRightIs(true)

1
在 Kotlin 中:
val params = mBinding.tvTotalAmount.layoutParams as RelativeLayout.LayoutParams

params.addRule(RelativeLayout.ALIGN_PARENT_END)

mBinding.tvTotalAmount.layoutParams = params

mBinding.tvTotalAmount.layoutParams = params


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接