【问题标题】:My scroll view doesn't scroll我的滚动视图不滚动
【发布时间】:2015-11-18 21:14:07
【问题描述】:

我有一个这样的 xml 视图,如您所见,我在其中有滚动视图,但滚动视图不滚动并且仅匹配屏幕大小,并且我无法在屏幕下看到数据。 这是我的代码: 代码已更新

 <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:fillViewport="true">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/aboveRelative"
        android:background="@color/darkOrange">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/category_text"
            android:textSize="23sp"
            android:textColor="@color/white"
            android:text="Category"
            android:paddingLeft="10sp" />
        <GridView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/gridview"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:verticalSpacing="0dp"
            android:horizontalSpacing="10dp"
            android:numColumns="2"
            />


    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/gray"
        android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/topVoices"
                android:textSize="23sp"
                android:text="@string/topVoices"

                android:paddingLeft="10sp" />

            <ListView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/trendlist"
                android:elevation="2dp"
                android:background="@color/white"
                android:layout_margin="10dp" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/moreTrend"
                android:layout_gravity="right"
                android:textSize="23sp"
                android:text="More"

                android:paddingLeft="10sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"


                android:id="@+id/newVoices"
                android:textSize="23sp"
                android:text="@string/newVoices"
                android:paddingLeft="10sp" />
            <ListView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/soundlist"
                android:background="@color/white"
                android:layout_below="@+id/newVoices"
                android:elevation="5dp"
                android:layout_margin="10dp" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/soundlist"
                android:id="@+id/moreNew"
                android:layout_gravity="right"
                android:textSize="23sp"

                android:text="@string/more"/>
            <LinearLayout
                android:id="@+id/yoursongs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_gravity="center"
                android:background="@color/white"
                android:padding="10dp">

            </LinearLayout>

    </LinearLayout>

    </LinearLayout>

我确定屏幕下有一些数据,但滚动视图不允许我滚动它并看到它们。

【问题讨论】:

  • 所以,它应该只有一个孩子,但查看你的代码,我认为你确实有它。另外,由于您使用的是RelativeLayout,您能否看到它是否以某种方式结束在循环中重新测量您的显示,因为它是滚动视图?而相对布局适应最大?我会尝试将所有RelativeLayouts 更改为LinearLayout,只是为了看看是否是这种情况。另外,你为什么不删除根处的 LinearLayout 并将 ScrollView 保留为根?
  • 我照你说的做了,但结果并没有什么不同。
  • 您在 ScrollView 中有一个 ListView,这可能不起作用?检查此链接:developer.android.com/intl/es/reference/android/widget/… 明确表示您不应在滚动视图中使用列表视图。

标签: android xml scrollview


【解决方案1】:

Scrollview 布局只能有一个子布局。您发布的 XML 中仍有两个线性布局子级。

这在 ScrollView 的 Android 开发者文档的第一段中有解释: http://developer.android.com/reference/android/widget/ScrollView.html

【讨论】:

  • 我把它们放在一个线性布局中。
【解决方案2】:

你的布局是错误的。这是你必须做的:

  1. 将您的 ScrollView layout_height 属性更改为 match_parent
    ScrollView 将占据整个屏幕并使其自身可滚动,这就是您需要它作为 match_parent 的原因。滚动发生在视图内部。
  2. 将 ScrollView 中的所有内容放在 LinearRelativeLayout 中。 (见下文)
    ScrollView 扩展了 FrameLayout,因此它只能有一个子视图。
    重要提示:此布局必须具有属性 layout_height设置为 wrap_content,否则 ScrollView 将不会有任何大于自身的内容可滚动。
  3. 我还会从 XML 中删除根布局,并将 ScrollView 保留为根。
    仅仅是因为它没有意义,而且需要 LayoutInflater 进行额外的工作。


编辑:您的布局也比它需要的更复杂
它让我相信你在 ScrollView 中没有一个孩子(你)。

不过,第 1 项应该可以解决您的问题。

【讨论】:

  • 我照你说的做了,但没有任何改变。
  • 你能用最新的 XML 更新你的问题吗?
  • 对不起,我的原始答案中有一项,但我删除了它,因为您的代码已经正确。那么当你改变我的建议时,你一定也无意中改变了它。查看最上面的 ScrollView 内的 LinearLayout。其layout_height 设置为match_parent。不可能,所以请将其更改为wrap_content(仅此而已)。
【解决方案3】:

终于,我找到了答案。问题来自滚动视图中的填充视图。如果你移除 fillviewport 并为 listview 设置固定大小,你可以滚动。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多