【发布时间】:2012-10-26 09:49:22
【问题描述】:
我正在使用此代码
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));
我可以使用什么样的参数布局选项来让它工作。
【问题讨论】:
-
这很奇怪,因为
View.setLayoutParams()和LayoutParams(int,int)在 API 11 之前就已经存在了。您可以导入不同的LayoutParams吗?试试这个:textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT ,ViewGroup.LayoutParams.WRAP_CONTENT)); -
这很奇怪,就像我不能使用 FILL_PARENT,eclipse 说 FILL_PARENT 已被弃用。而且我在代码中看不到任何错误。
-
你的最小和目标 sdk 是什么??
-
minsdkversion = 8 和 targetsdkversion = 15 @Girish
-
尝试使用常量值意味着
Constant Value: -1 (0xffffffff)FILL_PARENT 所以使用-1 代替LayoutParams.FILL_PARENTdeveloper.android.com/reference/android/view/…
标签: android android-layout textview layoutparams