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

发布于 2024-10-11 02:39:01 字数 546 浏览 8 评论 0原文

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

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

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

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

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

I have a relative layout which I am creating programmatically:

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

Now I have two buttons which I want to add in this relative layout. But the problem is both buttons are being shown on the left of the RelatiiveLayout overlapping on each other.

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

Now I want to know how can I programmatically set the the android:layout_alignParentRight="true"
or android:layout_toLeftOf="@id/btn" attribute of buttons as we do in the xml?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

束缚m 2024-10-18 02:39:02

您可以使用 View.getLayoutParams 从代码中访问任何 LayoutParams。您只需非常清楚您访问的 LayoutParams 即可。这通常是通过检查包含的 ViewGroup 来实现的,如果它有一个 LayoutParams 内部子级,那么这就是您应该使用的。在您的情况下,它是RelativeLayout.LayoutParams。您将使用 RelativeLayout.LayoutParams# addRule(int 动词)RelativeLayout.LayoutParams#addRule(int verb, intanchor)

您可以通过代码访问它:

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

You can access any LayoutParams from code using View.getLayoutParams. You just have to be very aware of what LayoutParams your accessing. This is normally achieved by checking the containing ViewGroup if it has a LayoutParams inner child then that's the one you should use. In your case it's RelativeLayout.LayoutParams. You'll be using RelativeLayout.LayoutParams#addRule(int verb) and RelativeLayout.LayoutParams#addRule(int verb, int anchor)

You can get to it via code:

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
往昔成烟 2024-10-18 02:39:02

要添加值为 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) 的快捷方式。

For adding a RelativeLayout attribute whose value is true or false use 0 for false and RelativeLayout.TRUE for true:

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

It doesn't matter whether or not the attribute was already added, you still use addRule(verb, subject) to enable/disable it. However, post-API 17 you can use removeRule(verb) which is just a shortcut for addRule(verb, 0).

沫雨熙 2024-10-18 02:39:02
  1. 您需要为
    需要参考的按钮:
    btn1.setId(1);
  2. 您可以使用 params 变量来
    将参数添加到您的布局中,我
    认为该方法是addRule(),检查
    出这个 android java 文档
    LayoutParams 对象。
  1. you need to create and id for the
    buttons you need to refference:
    btn1.setId(1);
  2. you can use the params variable to
    add parameters to your layout, i
    think the method is addRule(), check
    out the android java docs for this
    LayoutParams object.
自控 2024-10-18 02:39:02

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)

Kotlin version:

Use these extensions with infix functions that simplify later calls

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
}

Then use them as infix functions calls:

view1 below view2
view1 leftOf view2
view1 alightParentRightIs true

Or you can use them as normal functions:

view1.below(view2)
view1.leftOf(view2)
view1.alightParentRightIs(true)
萤火眠眠 2024-10-18 02:39:02

在 Kotlin 中:

val params = mBinding.tvTotalAmount.layoutParams asrelativeLayout.LayoutParams

params.addRule(RelativeLayout.ALIGN_PARENT_END)

mBinding.tvTotalAmount.layoutParams = params

In Kotlin:

val params = mBinding.tvTotalAmount.layoutParams as RelativeLayout.LayoutParams

params.addRule(RelativeLayout.ALIGN_PARENT_END)

mBinding.tvTotalAmount.layoutParams = params

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文