动态地添加按钮中的RelativeLayout到的LinearLayout [英] Adding Buttons dynamically in RelativeLayout to LinearLayout
问题描述
当用户输入一个字,他创造了许多按钮
等于字的长度。例如:如果用户输入AAAA
他将边创建4个按钮
,边,第一排。然后,如果用户输入BB
,他将创造2 按钮
,并排,第二排。和CCC
他创造了3 按钮
...
图像来演示:
我动态创建一个 RelativeLayout的
,然后动态地添加按钮
到布局。最后我想补充 RelativeLayout的
来我现有的的LinearLayout
。但问题是,只有一个按钮
是每行补充说。而我的计划目前看起来是这样的:
有人请我解决这个问题?
code:
最后的LinearLayout的LinearLayout =(的LinearLayout)findViewById(R.id.ll_bttn_words); 最后LinearLayout.LayoutParams LLP =新LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT); button_test.setOnClickListener(
新View.OnClickListener()
{
公共无效的onClick(查看视图)
{
的RelativeLayout的RelativeLayout =新的RelativeLayout(view.getContext()); RelativeLayout.LayoutParams RLP =新RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
INT大小= enter_txt.getText()的toString()长()。 //按钮的用户输入的数目 INT ID = 1; 的for(int i = 0; I<大小;我++)
{
按钮myButton的=新按钮(view.getContext()); myButton.setBackgroundResource(R.drawable.button); myButton.setId(ID); rlp.addRule(RelativeLayout.RIGHT_OF,myButton.getId()); relativeLayout.addView(myButton的,RLP); ID ++;
} linearLayout.addView(RelativeLayout的,LLP);
rlp.addRule(RelativeLayout.RIGHT_OF,myButton.getId());
该行表示,为myButton应添加到myButton的权利,这没有任何意义。
要解决这个简单的方法是使用下面的行,而不是
rlp.addRule(RelativeLayout.RIGHT_OF,myButton.getId() - 1);
但是,这不是要做到这一点的最好办法,你应该使用的LinearLayout与水平方向相反。
When the user inputs a word, he creates a number of Buttons
equal to the length of the word. For example: if user inputs "aaaa"
he will create 4 Buttons
, side by side, in the first row. Then if the user enters "bb"
he will create 2 Buttons
, side by side, in the second row. And "ccc"
he creates 3 Buttons
...
Image to demonstrate:
I dynamically create a RelativeLayout
, then dynamically add Buttons
to that layout. And finally I add the RelativeLayout
to my existing LinearLayout
. But the problem is, only one Button
is added per row. And my program currently looks like this:
Can someone please me fix this problem?
CODE:
final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_bttn_words);
final LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
button_test.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
RelativeLayout relativeLayout = new RelativeLayout(view.getContext());
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
int size = enter_txt.getText().toString().length(); //the user input number of buttons
int id = 1;
for (int i=0; i<size; i++)
{
Button myButton = new Button(view.getContext());
myButton.setBackgroundResource(R.drawable.button);
myButton.setId(id);
rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId());
relativeLayout.addView(myButton, rlp);
id++;
}
linearLayout.addView(relativeLayout, llp);
rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId());
This line says that myButton should be added to right of myButton, which doesn't make any sense.
simple way to resolve this is to use the following line instead
rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId()-1);
But this isn't the best way to do this, you should use LinearLayout with horizontal orientation instead.
这篇关于动态地添加按钮中的RelativeLayout到的LinearLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!