简体   繁体   中英

Adding Buttons dynamically in RelativeLayout to 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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM