如何创建textview的确切数量并将它们放入tableview?

时间:2012-11-03 12:30:34

标签: android textview android-linearlayout tablelayout

我有这段代码

public class MainActivity extends Activity {

    TextView textview;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    String hallo = "blabla " + "jojo " + "lol " + "\n" + "doj " + "drasl " + "\n"; 
    InputStream is = new ByteArrayInputStream(hallo.getBytes());
    BufferedReader in = new BufferedReader(new InputStreamReader(is));
    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);

    String line;
    try {
        while ((line = in.readLine()) != null) {
            String[] RowData = line.split(" ");
            String eitt = RowData[0];
            String tvo = RowData[1];

            TextView textView = new TextView(this);
            textView.setText(line);

            linearLayout.addView(textView);
        }
    } catch (IOException e1) {
        e1.printStackTrace();
    }
}
}

使用此xml布局

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        </LinearLayout>

</ScrollView>

我想将字符串的这些部分插入到表格布局中,这样当我的文本片段被打印出来时,它们会出现在漂亮的列中。它的工作方式,它只是打印行的字符串行而不是将其放入列。同样非常重要的是要知道我不知道字符串中会有多少行,所以我不能只制作4个textview框,因为可能有50 og一百行,所以我需要制作一个表视图中的textview框的数量,其中i是行数

1 个答案:

答案 0 :(得分:2)

尝试将setLayoutParams添加到TextView

//Your code...
String line;
    try {
        while ((line = in.readLine()) != null) {
            String[] RowData = line.split(" ");
            String eitt = RowData[0];
            String tvo = RowData[1];

            TextView textView = new TextView(this);
            textView.setText(line);
            textView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT
           ,LayoutParams.WRAP_CONTENT));
            linearLayout.addView(textView);
        }
    } catch (IOException e1) {
        e1.printStackTrace();
    }

//Your code...