当前位置: 代码迷 >> Android >> 在Android中滚动查看
  详细解决方案

在Android中滚动查看

热度:82   发布时间:2023-08-04 11:40:21.0

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

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>

您需要将可扩展的listview和gridview添加到一种常见的相对布局中,这将解决您的问题。

<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/>标记只能容纳一个孩子。 意味着您不能像在此处那样在<Scrollview/>直接定义多个孩子。

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

请参阅此链接。

它说...

ScrollView是一个FrameLayout,这意味着您应该在其中放置一个包含所有要滚动内容的子级; 这个孩子本身可能是具有复杂对象层次结构的布局管理器。

解决方案:

您必须将两个视图合并到任何其他父视图中,例如<LinearLayout/><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>
  相关解决方案