如何在 Android 中动态地将 TextView 添加到 LinearLayout? [英] How to add a TextView to a LinearLayout dynamically in Android?

查看:15
本文介绍了如何在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆