2

我正在使用此代码

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);
            textView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT
           ,LayoutParams.WRAP_CONTENT));
            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 是行数。但我总是收到这个错误,调用需要 api 级别 11,我使用的是 api 级别 10。错误是因为这条线

textView.setLayoutParams(newLayoutParams(LayoutParams.FILL_PARENT
           ,LayoutParams.WRAP_CONTENT));

我可以使用什么样的参数布局选项来让它工作。

4

2 回答 2

3

您没有显示导入,但您可能使用了错误的 LayoutParams 导入(如 meowmeow 建议的那样)。看起来您可能想改用以下导入。

import android.view.ViewGroup.LayoutParams;
于 2014-03-18T23:34:26.597 回答
0

我只是有同样的问题。仔细检查您的导入,看看您没有导入不合适的 LayoutParams。当它应该是 LinearLayout.LayoutParams 时,我最终导入了像 Actionbar.LayoutParams 这样的随机东西。

于 2013-11-10T12:30:16.700 回答