0

我有这个代码

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 一百行,所以我需要制作一个 i表格视图中文本视图框的数量,其中 i 是行数

4

1 回答 1

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...
于 2012-11-03T12:37:34.600 回答