与参数编程方式添加按钮? [英] Add Button programmatically with parameters?
问题描述
基本上,我有这样的布局结构:<&RelativeLayout的GT; <&RelativeLayout的GT; <的TextView /> < / RelativeLayout的> <&滚动型GT; <&RelativeLayout的GT; ...< / RelativeLayout的> < /滚动型> < / RelativeLayout的>
,我想以编程方式添加一个按钮。该按钮应该是&LT内部; RelativeLayout的>
,这是&LT内部,滚动型>
。另外,我需要它来对准底部和CENTER_VERTICAL。
我真的AP preciate任何提示/例;)谢谢!
附:虽然有计算器上很多类似的问题,没有一个答案帮我...
Basically, I have this layout structure: <RelativeLayout> <RelativeLayout> <TextView /> </RelativeLayout> <ScrollView> <RelativeLayout> ... </RelativeLayout> </ScrollView> </RelativeLayout>
and I want to add a button programmatically. This button should be inside the <RelativeLayout>
, which is inside the <ScrollView>
. Also, I need it to be align to the bottom and CENTER_VERTICAL.
I would really appreciate any hints/examples ;) Thanks!
P.S. Although, there are many similar questions on stackoverflow, none of the answers helped me...
推荐答案
首先,你需要给在XML中相对布局的ID:机器人:ID =@ + ID / myLayout
。
然后,在Java中code:
First you need to give your relative Layout in XML an ID: android:id="@+id/myLayout"
.
Then in Java code:
Button b = new Button(this);
RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rl.addRule(RelativeLayout.ALIGN_BOTTOM);
b.setLayoutParams(rl);
((RelativeLayout) findViewById(R.id.myLayout)).addView(b);
这篇关于与参数编程方式添加按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!