[英]Android Programming: How can I add a text view on the screen each time I press a button on the screen?
I'm new to android programming and am wondering if anyone can help me on this one: How can I add a textView on the screen each time I press a button on the screen? 我是android编程的新手,我想知道是否有人可以帮助我:每次按屏幕上的按钮时,如何在屏幕上添加textView?
The button is already generated from the XML. 该按钮已经从XML生成。 However, the textview need to be produced in runtime.
但是,textview需要在运行时中生成。
You can produce any view at runtime by doing a process like this. 您可以通过执行以下过程在运行时生成任何视图。
LinearLayout ll = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.common_list)
TextView tv = new TextView(this);
tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
tv.setText("sample text");
ll.addView(tv);
setContentView(ll);
This worked for me: 这为我工作:
ViewGroup layout = (ViewGroup) findViewById(R.id.your_layout_id);
TextView tv = new TextView(this);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setText("Added tv");
layout.addView(tv);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.