您需要创建新的 TextViews 以放入 TextView 数组,并且您正在跳过第一个索引 ( pairs[0]
),这将在以后导致麻烦:
pairs=new TextView[num_match];
for(int l=0; l<num_match; l++)
{
pairs[l] = new TextView(this);
pairs[l].setTextSize(15);
pairs[l].setLayoutParams(lp);
pairs[l].setId(l);
pairs[l].setText(m1[l + 1]+" - "+m2[l + 1]);
myLayout.addView(pairs[l]);
}
根据您的评论,我包含了这个简单的工作示例来帮助您:
public class Example extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout myLayout = (LinearLayout) findViewById(R.id.my_layout);
LayoutParams lp = new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView[] pairs=new TextView[4];
for(int l=0; l<4; l++)
{
pairs[l] = new TextView(this);
pairs[l].setTextSize(15);
pairs[l].setLayoutParams(lp);
pairs[l].setId(l);
pairs[l].setText((l + 1) + ": something");
myLayout.addView(pairs[l]);
}
}
}
随着布局main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />