简体   繁体   中英

How can I dynamically create and place a button before a button?

I have learned how to add a button after an existing button dynamically, but is it possible to dynamically insert a button before something?

When "+ Add Person" is clicked, it will insert a button in between the very first button and the "+ Add Person" button.

Then it will extend the gridView vertically when it the maximum columns have been met.

I have a solution for your question. Here they are :

  1. Create an ArrayList, which inside you store Button.
  2. In your layout create LinearLayout with orientation horizontal.
  3. When click add client, add Button to the ArrayList.
  4. Then remove all the Button inside LinearLayout.
  5. Add the Button from ArrayList with LinearLayout's addView.
  6. After the looping for adding button is done, then add the + Add person button.

For the expand to next row, like screenshot number 2. You need to create logic to see if the ArrayList is exceed your maximum button count. Then the structure of layout will be different also. It will have :

  1. LinearLayout with vertical orientation.
  2. The LinearaLayout vertical will have addView based on the number of button. The view added is LinearLayout with horizontal orientation.
  3. Then after that follow the previous button to add button in linear layout.

Please ask if you need further explanation. Thank you !.

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