简体   繁体   English

如何动态地向/从linearLayout添加/删除textViews?

[英]How dynamically add/delete textViews to/from linearLayout?

I have db with name of countries(for example). 我有国家名称的数据库(例如)。 My first goal is dynamically add textView for (each country) to linearLayout (android:layout_width="match_parent" android:layout_height="match_parent") . 我的第一个目标是将(每个国家/地区)的textView动态添加到linearLayout (android:layout_width="match_parent" android:layout_height="match_parent") In fact adding is not a problem, but problem if the orientation of layout is horizontal and there is to much countries, but the width of layout is not enough to show all of them and they are hide! 实际上,添加不是问题,但是如果布局的方向是horizontal并且存在很多国家,那么问题就出现了,但是布局的宽度不足以显示所有这些国家并且它们是隐藏的!

So i need to add textViews dynamically, but if there is not enough space for new textView - add it in the next line or create new linearLayout. 所以我需要动态添加textViews,但是如果没有足够的空间容纳新的textView,请在下一行添加它或创建新的linearLayout。

I need to create new textView for each country cos than i want to make clickListener for each of them, and there is second goal... 我需要为每个国家/地区cos创建新的textView,而不是为每个国家/地区创建clickListener,这是第二个目标...

the second goal is delete some of that textView by clicking on it, and the other textView must relocate to that empty space. 第二个目标是通过单击它来删除某些textView,而另一个textView必须重新定位到该空白区域。 Hope that my explanation is clear :) if not i will try another one. 希望我的解释清楚:)如果没有,我会再尝试一个。

so, i have idea how to add textView: every time when i add new textView - count the length of all previously added ones with this one and compare it with length of linearLayout, if the length of all textViews is less - add to this linLayout, else - create new lin layout and add textView there. 所以,我知道如何添加textView:每次我添加新的textView时-计数所有以前添加的textView的长度,如果所有textViews的长度都较小,则将其与linearLayout的长度进行比较-添加到此linLayout ,否则-创建新的lin布局并在其中添加textView。 I think this could work, it it looks ugly :) I hope there are must be more simply and pretty solution! 我认为这可行,看起来很丑:)我希望必须有更简单,更漂亮的解决方案!

Talking about dynamically deleting textViews from layout - I have no idea how to do this correctly. 谈论从布局中动态删除textViews-我不知道如何正确执行此操作。

So I will be glad any solutions and ideas, thanks! 因此,我将很高兴提供任何解决方案和想法,谢谢!

EDIT here is example how I want it looks like in the end: 编辑这里是示例,我希望它最终看起来像什么: 在此处输入图片说明

LinearLayout is usually used for fixed childs. LinearLayout通常用于固定子项。

For dynamic and large number of childs, you should use ListView or RecyclerView , which is exactly designed for what you described. 对于动态的大量孩子,您应该使用ListViewRecyclerView ,它们是针对您所描述的内容而专门设计的。

More than that, ListView and RecyclerView can reuse the child views, ie TextView for your case, which is needed, because view objects are heavy to create and keep in memory. 不仅如此, ListViewRecyclerView可以重用子视图,即您的案例所需的TextView,因为视图对象难以创建并保留在内存中。

Edit: 编辑:

Given your image and replies in comment. 给出您的图片并回复评论。 I would suggest you to use TextView with ClickableSpan, and set update the whole Text on click. 我建议您将TextView与ClickableSpan结合使用,并在单击时设置更新整个Text。

You can check ClickableSpan in the link below. 您可以在下面的链接中检查ClickableSpan。

http://www.programcreek.com/java-api-examples/index.php?api=android.text.style.ClickableSpan http://www.programcreek.com/java-api-examples/index.php?api=android.text.style.ClickableSpan

You can use flowlayout for that. 您可以为此使用flowlayout。 Link for flowlayout is https://github.com/ApmeM/android-flowlayout . 用于flowlayout的链接是https://github.com/ApmeM/android-flowlayout Just inflate your views inside flowlayout. 只需在flowlayout中增加您的视图即可。 You don't have to do any calculation when inflating. 膨胀时无需进行任何计算。 If there is space available for textviews then they will be added horizontally else in the next line. 如果有足够的空间用于文本视图,则将它们水平添加到下一行。

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

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