【发布时间】:2014-11-06 14:31:30
【问题描述】:
当用户输入一个单词时,他会创建一个等于单词长度的Buttons。例如:如果用户输入"aaaa",他将在第一行并排创建 4 个Buttons。然后,如果用户输入"bb",他将在第二行并排创建 2 个Buttons。而"ccc" 他创建了 3 个Buttons...
要演示的图片:
我动态创建一个RelativeLayout,然后动态地将Buttons 添加到该布局中。最后我将RelativeLayout 添加到我现有的LinearLayout 中。但问题是,每行只添加一个Button。我的程序目前看起来像这样:
谁能帮我解决这个问题?
代码:
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);
【问题讨论】:
标签: android button android-linearlayout android-relativelayout