如何以编程方式设置相对布局中按钮的layout_align_parent_right属性?
我有一个以编程方式创建的相对布局:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用
View.getLayoutParams
从代码中访问任何LayoutParams
。您只需非常清楚您访问的LayoutParams
即可。这通常是通过检查包含的ViewGroup
来实现的,如果它有一个LayoutParams
内部子级,那么这就是您应该使用的。在您的情况下,它是RelativeLayout.LayoutParams
。您将使用RelativeLayout.LayoutParams# addRule(int 动词)
和RelativeLayout.LayoutParams#addRule(int verb, intanchor)
您可以通过代码访问它:
You can access any
LayoutParams
from code usingView.getLayoutParams
. You just have to be very aware of whatLayoutParams
your accessing. This is normally achieved by checking the containingViewGroup
if it has aLayoutParams
inner child then that's the one you should use. In your case it'sRelativeLayout.LayoutParams
. You'll be usingRelativeLayout.LayoutParams#addRule(int verb)
andRelativeLayout.LayoutParams#addRule(int verb, int anchor)
You can get to it via code:
要添加值为 true 或 false 的
RelativeLayout
属性,请使用0
表示 false,使用RelativeLayout.TRUE
表示 true:无论是还是尚未添加该属性,您仍然可以使用 addRule(verb, subject) 来启用/禁用它。但是,在 API 17 之后,您可以使用
removeRule(verb)
,它只是addRule(verb, 0)
的快捷方式。For adding a
RelativeLayout
attribute whose value is true or false use0
for false andRelativeLayout.TRUE
for 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 useremoveRule(verb)
which is just a shortcut foraddRule(verb, 0)
.需要参考的按钮:
btn1.setId(1);
将参数添加到您的布局中,我
认为该方法是
addRule()
,检查出这个 android java 文档
LayoutParams
对象。buttons you need to refference:
btn1.setId(1);
add parameters to your layout, i
think the method is
addRule()
, checkout the android java docs for this
LayoutParams
object.Kotlin 版本:
将这些扩展与中缀函数结合使用,以简化以后的调用
然后将它们用作中缀函数调用:
或者您可以将它们用作普通函数:
Kotlin version:
Use these extensions with infix functions that simplify later calls
Then use them as infix functions calls:
Or you can use them as normal functions:
在 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