动态添加一个TextView - 机器人 [英] Dynamically Add A TextView - Android

查看:114
本文介绍了动态添加一个TextView - 机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 公共类myTextSwitcher延伸活动{

    TextView的私人会将myText;
    公共myTextSwitcher(字符串字符串){

        // =会将myText新的TextView(本);
        //myText.setText("My文本);
    }
}
 

解决方案

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

您正在试图做的是动态配置的图什么。看到这里href="http://www.dreamin$c$c.net/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 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆