如何在Android中动态地将TextView添加到LinearLayout? [英] How to add a TextView to a LinearLayout dynamically in Android?
本文介绍了如何在Android中动态地将TextView添加到LinearLayout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试动态地将TextView
添加到LinearLayout
中,例如下面的代码,但是当我运行该应用程序时它不会出现吗?
I try to add a TextView
to a LinearLayout
dynamically such as in the following code, but it doesn't appear when I run the application?
setContentView(R.layout.advanced);
m_vwJokeLayout=(LinearLayout) this.findViewById(R.id.m_vwJokeLayout);
m_vwJokeEditText=(EditText) this.findViewById(R.id.m_vwJokeEditText);
m_vwJokeButton=(Button) this.findViewById(R.id.m_vwJokeButton);
TextView tv=new TextView(this);
tv.setText("test");
this.m_vwJokeLayout.addView(tv);
出什么问题了?
推荐答案
LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv=new TextView(this);
tv.setLayoutParams(lparams);
tv.setText("test");
this.m_vwJokeLayout.addView(tv);
您可以根据需要更改 lparams
这篇关于如何在Android中动态地将TextView添加到LinearLayout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文