如何添加一个TextView到Android中动态地的LinearLayout? [英] How can I add a TextView to a LinearLayout dynamically in Android?
本文介绍了如何添加一个TextView到Android中动态地的LinearLayout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试一个TextView添加到的LinearLayout动态,如下面的code,但是当我运行该应用程序也不会出现?
的setContentView(R.layout.advanced);
m_vwJokeLayout =(的LinearLayout)this.findViewById(R.id.m_vwJokeLayout);
m_vwJokeEditText =(EditText上)this.findViewById(R.id.m_vwJokeEditText);
m_vwJokeButton =(按钮)this.findViewById(R.id.m_vwJokeButton);
TextView的电视=新的TextView(本);
tv.setText(测试);
this.m_vwJokeLayout.addView(电视);
这是什么问题?
解决方案
的LayoutParams lparams =新的LayoutParams(
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
TextView的电视=新的TextView(本);
tv.setLayoutParams(lparams);
tv.setText(测试);
this.m_vwJokeLayout.addView(电视);
您可以修改 lparams 根据您的需要
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);
What's the problem?
解决方案
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);
You can change lparams according to your needs
这篇关于如何添加一个TextView到Android中动态地的LinearLayout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文