我想根据for循环的条件创建多个TextView。 如何以编程方式进行?
模板:
app name: facebook
duration: 2
app name: messaging
duration: 4
app name: temple run
duration: 2
答案 0 :(得分:0)
你可以这样create a TextView:
TextView myTextView = new TextView(this);
myTextView.setText("A TextView");
然后您可以将此视图添加到您的布局
答案 1 :(得分:0)
在xml的布局中添加id
字段。参考班上的布局。创建视图并将其添加到布局。
LinearLayout linearLayout = (LinearLayout) findViewById (R.id.YourID);
TextView textView = new TextView (this);
textView. setText ("YourText");
linearLayout. addView (textView);
答案 2 :(得分:0)
您可以使用此代码以编程方式创建textview:
TextView mTextView = new TextView(this);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mTextView.setLayoutParams(params);
rootView.addView(mTextView, 0);
答案 3 :(得分:0)
我对此的评价:
你有两个数组1.appname 2. duration然后尝试下面的代码。
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.YourID);
for (int i = 0; i < appname.length; i++)
{
TextView textAppname = new TextView(this);
TextView textDuration = new TextView(this);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
textAppname.setLayoutParams(params);
textDuration.setLayoutParams(params);
textAppname.setText(appname[i]);
textDuration.setText(duration[i]);
linearLayout.addView(textAppname);
linearLayout.addView(textDuration);
}