Adding Buttons dynamically in RelativeLayout to LinearLayout

ぃ、小莉子 提交于 2020-01-06 13:53:07

问题


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

回答1:


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.




回答2:


The structure should be simple

Just need to add your buttons in 3 different linear layout with orientation horizontal.

Like

<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>
}
}



回答3:


You guys are right. It is much easier using a LinearLayout. For those interested

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


来源:https://stackoverflow.com/questions/26772569/adding-buttons-dynamically-in-relativelayout-to-linearlayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!