简体   繁体   English

如何动态创建按钮并将其放置在按钮之前?

[英]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. 然后,当达到最大列数时,它将垂直扩展gridView。

I have a solution for your question. 我有一个解决您的问题的方法。 Here they are : 他们来了 :

  1. Create an ArrayList, which inside you store Button. 创建一个ArrayList,在其中存储Button。
  2. In your layout create LinearLayout with orientation horizontal. 在您的布局中,创建水平方向的LinearLayout。
  3. When click add client, add Button to the ArrayList. 单击添加客户端时,将Button添加到ArrayList。
  4. Then remove all the Button inside LinearLayout. 然后删除LinearLayout中的所有Button。
  5. Add the Button from ArrayList with LinearLayout's addView. 使用LinearLayout的addView从ArrayList添加按钮。
  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. 对于扩展到下一行,如2号屏幕截图。您需要创建逻辑以查看ArrayList是否超过最大按钮数。 Then the structure of layout will be different also. 这样布局的结构也将不同。 It will have : 它将有 :

  1. LinearLayout with vertical orientation. 垂直方向的LinearLayout。
  2. The LinearaLayout vertical will have addView based on the number of button. LinearaLayout垂直控件将具有基于按钮数量的addView。 The view added is LinearLayout with horizontal orientation. 添加的视图是水平方向的LinearLayout。
  3. Then after that follow the previous button to add button in linear layout. 然后在那之后按照上一个按钮添加线性布局的按钮。

Please ask if you need further explanation. 请询问您是否需要进一步的解释。 Thank you !. 谢谢 !。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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