如何以编程方式设置相对布局按钮的layout_align_parent_right属性? [英] How to programmatically set the layout_align_parent_right attribute of a Button in Relative Layout?
问题描述
您好! 我有我创建一个编程相对布局:
HI! 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);
现在我有两个按钮,我想在这个相对布局添加。但问题是两个按钮被显示在上彼此RelatiiveLayout重叠的左侧。
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);
现在我想知道我怎么可以编程设置的安卓layout_alignParentRight =真
或安卓?layout_toLeftOf =@ ID / BTN按钮
属性,因为我们做的XML
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?
问候 FAS
推荐答案
您可以访问任何的LayoutParams
使用code View.getLayoutParams
。你一定要非常清楚什么的LayoutParams
您的访问。这通常是通过检查获得的含的ViewGroup
如果它有一个的LayoutParams
内在小孩那么这就是你应该使用的。你的情况是 RelativeLayout.LayoutParams
。您将使用<一个href="http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#addRule%28int%29"><$c$c>RelativeLayout.LayoutParams#addRule(int动词) 和<一href="http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#addRule%28int%2C%20int%29"><$c$c>RelativeLayout.LayoutParams#addRule(int动词,诠释锚)
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)
您可以通过code得到它:
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
这篇关于如何以编程方式设置相对布局按钮的layout_align_parent_right属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!