动态添加一个TextView

编程入门 行业动态 更新时间:2024-10-25 20:26:42
本文介绍了动态添加一个TextView - 机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何一个TextView动态地添加到这个?注释掉code不起作用。

公共类myTextSwitcher延伸活动{     TextView的私人会将myText;     公共myTextSwitcher(字符串字符串){         // =会将myText新的TextView(本);         //myText.setText("My文本);     } }

解决方案

您正在创建一个文本视图,并设置它的价值,但你不指定地点和方式应显示。你会将myText对象需要有某种这将使其可见的容器。

您正在试图做的是动态配置的图什么。看到这里href="www.dreamin$c$c/forums/topic/130521-android-part-iii-dynamic-layouts/" rel="nofollow">很好的入门文章一。从文章:

//这是在哪里以及如何在视图中使用 TextView的电视=新的TextView(本); tv.setText(FTW动态布局!); ll.addView(电视); //这部分是那里的容器获得有线在一起 滚动型SV =新的滚动型(本); 的LinearLayout LL =新的LinearLayout(本); ll.setOrientation(LinearLayout.VERTICAL); sv.addView(Ⅱ);

How can I dynamically add a TextView to this? The commented out code doesn't work.

public class myTextSwitcher extends Activity { private TextView myText; public myTextSwitcher(String string){ //myText = new TextView(this); //myText.setText("My Text"); } }

解决方案

You're creating a text view and setting its value but you're not specifying where and how it should be displayed. Your myText object needs to have a container of some sort which will make it visible.

What you're trying to do is dynamically layout a view. See here for a good starter article. From the article:

// This is where and how the view is used TextView tv = new TextView(this); tv.setText("Dynamic layouts ftw!"); ll.addView(tv); // this part is where the containers get "wired" together ScrollView sv = new ScrollView(this); LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); sv.addView(ll);

更多推荐

动态添加一个TextView

本文发布于:2023-11-06 21:46:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564762.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:动态   TextView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!