简体   繁体   English

Android编程:每次按下屏幕上的按钮时,如何在屏幕上添加文本视图?

[英]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.

相关问题 如何将按钮同时限制在屏幕底部和文本视图 - How can I constrain a button to the bottom of the screen and a text view at the same time 每次按下其他活动的按钮时,如何添加列表视图项? - How can i add a listview item each time i press a button from another activity? 当我按下“主页”按钮或“后退”按钮或“锁定屏幕”时,如何在后台运行媒体播放器 - How can i run media player in background when i press Home button or Back button or Locked Screen 如何在android中检测按钮的时间按下持续时间? - How can I detect time press duration of button in android? 如何检查我的Android应用程序上每个屏幕的时间 - How can i check time taken for each screen on my android app 我每次使用Android Studio按下按钮时如何使1个按钮播放随机声音 - How do i make 1 button play random sounds each time i press the button using android studio 如何将画布视图和按钮放置在同一屏幕上? - How can I place canvas view and button on the same screen? 如何在屏幕底部添加具有回收器视图的按钮。 这样按钮就不会隐藏回收器视图的最后一个元素 - How can I add button at the bottom of the screen having a recycler View. so that the button don't hide the last element of recycler view 如何在每次单击按钮时使用自定义适配器将项目添加到列表视图? - how can i add items to an list view with custom adapter each time i click on the button? 当我按下Android上的“后退”按钮时,如何避免Web视图出现白屏? - How to avoid white screen in a webview when I press the back button on Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM