簡體   English   中英

Android編程:每次按下屏幕上的按鈕時,如何在屏幕上添加文本視圖?

[英]Android Programming: How can I add a text view on the screen each time I press a button on the screen?

我是android編程的新手,我想知道是否有人可以幫助我:每次按屏幕上的按鈕時,如何在屏幕上添加textView?

該按鈕已經從XML生成。 但是,textview需要在運行時中生成。

您可以通過執行以下過程在運行時生成任何視圖。

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);

這為我工作:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM