将字符串数组添加到线性布局

时间:2011-07-21 13:13:42

标签: android android-layout android-linearlayout

我的代码如下。我正在显示细节,但不是以正确的方式。你能帮我吗?

TextView tv1=new TextView(this);
tv1.setText("DisplayName:"+strDisplayName);

TextView tv2=new TextView(this);
tv2.setText("Email:"+strEmail);

TextView tv3=new TextView(this);
tv3.setText("FirstName:"+strFirstName);

TextView tv4=new TextView(this);
tv4.setText("LastName:"+strLastName);

TextView tv5=new TextView(this);
tv5.setText("CreatedDate:"+strCreatedDate);

mProfileLayout.addView(tv1, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mProfileLayout.addView(tv2, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mProfileLayout.addView(tv3, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mProfileLayout.addView(tv4, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mProfileLayout.addView(tv5, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

1 个答案:

答案 0 :(得分:0)

你的xml中是否有所有这些TextView?如果您这样做,您可以使用

访问它们
TextView tvX = (TextView)findViewById(R.id.textviewXid);

然后只需设置文字

tvX.setText(string);

所以你不必使用mProfileLayout.addView(....);