在RelativeLayout中动态添加按钮到LinearLayout

时间:2014-11-06 05:50:06

标签: android button android-linearlayout relativelayout

当用户输入单词时,他会创建一个等于单词长度的Buttons个数。例如:如果用户输入"aaaa",他将在第一行中并排创建4 Buttons。然后,如果用户输入"bb",他将在第二行中并排创建2 Buttons"ccc"他创建了3 Buttons ...

要展示的图片:

enter image description here

我动态创建RelativeLayout,然后动态添加Buttons到该布局。最后,我将RelativeLayout添加到现有的LinearLayout。但问题是,每行只添加一个Button。我的程序目前看起来像这样:

enter image description here

有人可以帮我解决这个问题吗?

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);

3 个答案:

答案 0 :(得分:2)

rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId());

这一行说myButton应该添加到myButton的右边,这没有任何意义。

解决此问题的简单方法是使用以下行代替

rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId()-1);

但这不是最好的方法,你应该使用水平方向的LinearLayout。

答案 1 :(得分:1)

结构应该简单

只需要将您的按钮添加到3个不同的线性布局中,方向水平。

喜欢

<Relative layout>{
<LinearLayout global container with vertical orientation >{
<LinearLayout for 'a' type buttons container with horizontal orientation>
<LinearLayout for 'b' type buttons container with horizontal orientation>
<LinearLayout for 'c' type buttons container with horizontal orientation>
}
}

答案 2 :(得分:0)

你们是对的。使用LinearLayout要容易得多。对于那些感兴趣的人

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)
                {
                    LinearLayout linearLayout2 = new LinearLayout(view.getContext());

                    linearLayout2.setOrientation(LinearLayout.HORIZONTAL);

                    LinearLayout.LayoutParams rlp = new LinearLayout.LayoutParams(
                                        LinearLayout.LayoutParams.WRAP_CONTENT,
                                        LinearLayout.LayoutParams.WRAP_CONTENT);      

                    int size = enter_txt.getText().toString().length();
                    for (int i=0; i<size; i++)
                    {
                        Button myButton = new Button(view.getContext());
                        myButton.setBackgroundResource(R.drawable.button);
                        linearLayout2.addView(myButton, rlp);
                     }
                     linearLayout.addView(linearLayout2, llp);