我有從網絡獲取一些數據並將其顯示在屏幕上的活動。 我使用滾動視圖,因爲它是長文本,我也想爲不同的數據使用不同的文本樣式,所以我使用了幾個不同樣式的textView並在活動屏幕上顯示它,我的問題是滾動視圖可以處理只有一個視圖,所以如何使用滾動來顯示不同風格的文本視圖,我試圖將LinearLayout添加到scrollView中,並將代碼中的所有textViews動態添加到此LinearLayout中,但我收到異常 - 滾動視圖只能託管一個直接的孩子。將不同的TextView添加到ScrollView中
下面的代碼:
/** this is the function, which called from the onClick method.
wanted data object contains 2 strings title message and the message itself.
When debug the code i can see that there's two String values in each loop.
but i cant add the linearLayout to my scrollView - exception ScrollView can host only one direct child */
private void showResult(ArrayList<WantedData> result) {
// TODO Auto-generated method stub
TextView title;
TextView data;
scrollLayout = (LinearLayout) findViewById(R.id.LlScrollView);
for (WantedData curr : result) {
if (curr.getTitle() == null) {
break;
}
title = new TextView(this);
title.setText(curr.getTitle());
scrollLayout.addView(title, LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
data = new TextView(this);
data.setText(curr.getData());
scrollLayout.addView(data, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
}
scroll.addView(scrollLayout, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
//at the onCreate method - scroll = (ScrollView) findViewById(R.id.SvShowTextFromServer);
}
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include
android:id="@+id/layout_reffernce"
layout="@layout/explore" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter City" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/EtCity"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_weight="0.14"
android:orientation="vertical" >
<requestFocus />
</EditText>
<Button
android:id="@+id/bSearchCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search" />
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter State" />
<EditText
android:id="@+id/EtState"
android:layout_width="253dp"
android:layout_height="wrap_content"
android:orientation="vertical" />
</LinearLayout>
<ScrollView
android:id="@+id/SvShowTextFromServer"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/LlScrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/backround"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</LinearLayout>
加入'TextView'請 – Jin35 2012-01-14 14:41:50
scrollLayout =新的LinearLayout(本)提供代碼; \t \t \t \t scroll.addView(scrollLayout,LayoutParams.FILL_PARENT, \t \t \t \t \t LayoutParams.WRAP_CONTENT); \t \t爲(WantedData CURR:結果){ \t \t \t如果(curr.getTitle()== NULL){ \t \t \t \t中斷; \t \t \t} \t \t \t標題=新的TextView(本); \t \t \t title.setText(curr.getTitle()); \t \t \t scrollLayout.addView(標題,LayoutParams.FILL_PARENT, \t \t \t \t \t LayoutParams.WRAP_CONTENT); \t \t \t data = new TextView(this); \t \t \t data.setText(curr.getData()); \t \t \t \t scrollLayout.addView(數據,LayoutParams.FILL_PARENT, \t \t \t \t \t LayoutParams.WRAP_CONTENT); – BoazGarty 2012-01-14 14:51:29