在运行时Android添加textview动态

时间:2015-07-01 06:42:32

标签: android

我想在Android上运行时添加Text-view,以在活动上显示我的数据库内容,每次都可以更改。 请帮忙

1 个答案:

答案 0 :(得分:1)

您可以像这样动态创建TextView,例如:

//create a TextView with Layout parameters according to your needs
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//if your parent Layout is relativeLayout, just change the word LinearLayout with RelativeLayout
TextView tv=new TextView(this);
tv.setLayoutParams(lparams);
tv.setText("test");
//get the parent layout for your new TextView and add the new TextView to it
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_example);
linearLayout.addView(tv);

tv.setText(...);行中,您可以设置从数据库中获取的文本。