【问题标题】:Scroll View in android在android中滚动视图
【发布时间】:2015-10-24 07:42:55
【问题描述】:

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

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>

【问题讨论】:

    标签: android listview gridview scrollview


    【解决方案1】:

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

    <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>
    

    【讨论】:

    • 我还有一个疑问,我需要在哪里放置我的视图寻呼机代码@parth bhayani
    • schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height=" match_parent">
    【解决方案2】:

    您收到错误的原因是因为&lt;Scrollview/&gt; 标签只能承载一个孩子。意味着您不能像您在此处所做的那样直接在 &lt;Scrollview/&gt; 中定义多个孩子。

    您在&lt;Scrollview/&gt; 中定义了&lt;Gridview/&gt;&lt;ExpandableListView/&gt;。这不是&lt;Scrollview/&gt; 的正常行为。

    参考这个链接..

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

    上面写着...

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

    解决方案:

    您必须在任何其他父视图中组合这两个视图,例如&lt;LinearLayout/&gt;&lt;RelativeLayout/&gt;

    示例代码:

    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-01
      • 1970-01-01
      相关资源
      最近更新 更多