Android - 试图制作滚动视图,它正在崩溃

时间:2012-04-21 14:48:11

标签: android android-layout

我有这个观点:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >

<ScrollView
     xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"    >    

<TextView
    android:id="@+id/page_exlain"    
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Lots of text here"
    android:layout_marginTop ="20dp" 
    />          



<TextView
    android:id="@+id/exec_summary_heading"    
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/light_best_blue"
    android:layout_marginTop ="10dp" 
        android:text="1) EXECUTIVE SUMMARY"
    />
<TextView
    android:id="@+id/exec_summary_text"    
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Lots of text goes here."
    android:layout_marginTop ="10dp"    
    />              


<TextView
    android:id="@+id/product_heading"    
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/light_best_blue"
    android:layout_marginTop ="10dp"    
        android:text="2) YOUR PRODUCT OR SERVICE"
    />
<TextView
    android:id="@+id/product_text"    
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Lots of text goes here......"
    android:layout_marginTop ="10dp"    
    />    



</ScrollView>

</LinearLayout>

这次崩溃。知道为什么吗?我不知道如何使整个页面可滚动。

谢谢!

4 个答案:

答案 0 :(得分:3)

scrollview只能有一个直接子项

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >

<ScrollView
     xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"    >    

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >

<TextView
    android:id="@+id/page_exlain"    
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Lots of text here"
    android:layout_marginTop ="20dp" 
    />          



<TextView
    android:id="@+id/exec_summary_heading"    
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/light_best_blue"
    android:layout_marginTop ="10dp" 
        android:text="1) EXECUTIVE SUMMARY"
    />
<TextView
    android:id="@+id/exec_summary_text"    
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Lots of text goes here."
    android:layout_marginTop ="10dp"    
    />              


<TextView
    android:id="@+id/product_heading"    
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/light_best_blue"
    android:layout_marginTop ="10dp"    
        android:text="2) YOUR PRODUCT OR SERVICE"
    />
<TextView
    android:id="@+id/product_text"    
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Lots of text goes here......"
    android:layout_marginTop ="10dp"    
    />    


</LinearLayout>
</ScrollView>

</LinearLayout>

答案 1 :(得分:2)

像这样使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >

<ScrollView
     xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"    >  

  <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >

<TextView
    android:id="@+id/page_exlain"    
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Lots of text here"
    android:layout_marginTop ="20dp" 
    />          



<TextView
    android:id="@+id/exec_summary_heading"    
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/light_best_blue"
    android:layout_marginTop ="10dp" 
        android:text="1) EXECUTIVE SUMMARY"
    />
<TextView
    android:id="@+id/exec_summary_text"    
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Lots of text goes here."
    android:layout_marginTop ="10dp"    
    />              


<TextView
    android:id="@+id/product_heading"    
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/light_best_blue"
    android:layout_marginTop ="10dp"    
        android:text="2) YOUR PRODUCT OR SERVICE"
    />
<TextView
    android:id="@+id/product_text"    
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Lots of text goes here......"
    android:layout_marginTop ="10dp"    
    />    

</LinearLayout>

</ScrollView>

</LinearLayout>

原因是Android中的ScrollView一次只能容纳1个孩子。所以你必须将你想要在容器中滚动的所有TextView放在容器中再次LinearLayout

答案 2 :(得分:2)

您需要将文字视图放在另一个布局中,例如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >

<ScrollView
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"    >    


<LinearLayout 
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >

<TextView
    android:id="@+id/page_exlain"    
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Lots of text here"
    android:layout_marginTop ="20dp" 
    />          



<TextView
    android:id="@+id/exec_summary_heading"    
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/light_best_blue"
    android:layout_marginTop ="10dp" 
        android:text="1) EXECUTIVE SUMMARY"
    />
<TextView
    android:id="@+id/exec_summary_text"    
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Lots of text goes here."
    android:layout_marginTop ="10dp"    
    />              


<TextView
    android:id="@+id/product_heading"    
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/light_best_blue"
    android:layout_marginTop ="10dp"    
        android:text="2) YOUR PRODUCT OR SERVICE"
    />
<TextView
    android:id="@+id/product_text"    
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Lots of text goes here......"
    android:layout_marginTop ="10dp"    
    />    

</LinearLayout>

</ScrollView>

</LinearLayout>

(您需要删除Scrollview上的行xmlns:android="http://schemas.android.com/apk/res/android"

答案 3 :(得分:0)

看看这些关于创建简单列表视图和复杂自定义列表视图的简单示例。

Creating scrolling ListView in android

这个更复杂。

Android listView with icons

在-机器人/