2012-01-14 78 views
1

我有从网络获取一些数据并将其显示在屏幕上的活动。 我使用滚动视图,因为它是长文本,我也想为不同的数据使用不同的文本样式,所以我使用了几个不同样式的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> 
+0

加入'TextView'请 – Jin35 2012-01-14 14:41:50

+0

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

回答

0

如果定义在XML LinearLayout你不必在代码中创建一个新的LinearLayout但你必须检索现有的以这种方式

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.LlScrollView); 

否则,您必须于r在您的XML中移除LinearLayout并通过代码添加所有内容。

+0

谢谢,我知道,但它不是我害怕的问题,我已经删除了代码中的创建,仍然得到异常滚动视图可以只托管一个直接的孩子..可能每个添加到LinearLayout的文本视图都像添加到滚动视图 – BoazGarty 2012-01-14 18:55:04

+0

@david:这很奇怪,报告'onCreate()'方法的所有代码 - 编辑你的问题 - – 2012-01-14 19:11:03

+0

谢谢你,编辑我的问题的代码。 – BoazGarty 2012-01-14 20:22:26

1

问题是在ScrollView中双重创建容器。你不应该在活动创建它,而是采取从已经定义XML:

LinearLayout scrollContainer = (LinearLayout)findViewById(R.id.LlScrollView); 
for (...) { 
    //create here some text 
    scrollLayout.addView(text); 
} 
+0

谢谢,我知道,但它不是问题我害怕,我删除了在代码创建和添加引用的XML文件,仍然得到异常,滚动视图可以只托管一个直接孩子..也许每个文本视图添加到LinearLayout就像添加到滚动视图 – BoazGarty 2012-01-14 19:00:46