您可以将字符串附加到 textview 并为添加的每个字符串附加空间,如下所示。您不需要文本视图数组。只需将字符串附加到相同的位置即可。
TextView tv= new TextView(MainActivtiy.this);
tv.setText("");
for(int i=0;i<textArray.length;i++)
{
tv.append(textArray[i]);
tv.append(" ");
}
layout.addView(tv);
编辑:
TextView tv= new TextView(MainActivtiy.this);
tv.setText("");
for(int i=0;i<textArray.length;i++)
{
SpannableString ss1= new SpannableString(textArray[i]);
ss1.setSpan(new MyClickableSpan(textArray[i]), 0, ss1.length(),
tv.append(ss1);
tv.append(" ");
}
layout.addView(tv);
MyClickableSpan 类
class MyClickableSpan extends ClickableSpan{
String clicked;
public MyClickableSpan(String string) {
// TODO Auto-generated constructor stub
super();
clicked =string;
}
public void onClick(View tv) {
Toast.makeText(MainActivity.this,clicked ,Toast.LENGTH_SHORT).show();
//do what is required
}
public void updateDrawState(TextPaint ds) {
ds.setColor(Color.BLUE);//set text color
//ds.setStrokeWidth(15f);
ds.setUnderlineText(true); // set to false to remove underline
}
}