滚动查看 android

Scroll View in android

首先,我很抱歉我的英语不好,如何显示我的可扩展列表视图和网格视图,当我尝试这样做时出现错误
渲染期间引发的异常:

Scroll View can host only one direct child
Exception details are logged in Window > Show View > Error Log

代码:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.samp.sampleproh.MainActivity"
android:orientation="vertical" >



 <ExpandableListView
    android:id="@+id/lvExp"
    android:layout_width="match_parent"
    android:layout_height="205dp"
    android:layout_weight="0.41"
    android:cacheColorHint="#00000000" >
</ExpandableListView>


    <GridView
   android:id="@+id/gridview"
   android:layout_width="fill_parent" 
   android:layout_height="fill_parent"
   android:columnWidth="90dp"
   android:numColumns="auto_fit"
   android:verticalSpacing="10dp"
   android:horizontalSpacing="10dp"
   android:stretchMode="columnWidth"
   android:gravity="center"/>
</ScrollView>

您需要将可展开的列表视图和网格视图添加到一个通用的相对布局中,这将解决您的问题。

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ExpandableListView
                android:id="@+id/lvExp"
                android:layout_width="match_parent"
                android:layout_height="205dp"
                android:layout_weight="0.41"
                android:cacheColorHint="#00000000"></ExpandableListView>

            <GridView
                android:id="@+id/gridview"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_below="@+id/lvExp"
                android:columnWidth="90dp"
                android:gravity="center"
                android:horizontalSpacing="10dp"
                android:numColumns="auto_fit"
                android:stretchMode="columnWidth"
                android:verticalSpacing="10dp" />
        </RelativeLayout>
    </ScrollView>
</RelativeLayout>

出现错误的原因是 <Scrollview/> 标签只能承载一个 child。意味着您不能像在此处那样直接在 <Scrollview/> 中定义多个 child。

您在 <Scrollview/> 中定义了 <Gridview/><ExpandableListView/>。这不是 <Scrollview/> 的正常行为。

参考这个link..

http://developer.android.com/reference/android/widget/ScrollView.html

它说...

A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects.

解决方法:

You have to combine both view inside any other parent view e.g. <LinearLayout/> or <RelativeLayout/>.

示例代码:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

            <ExpandableListView/>
            <GridView/>

        <LinearLayout/>

</ScrollView>