【问题标题】:android scrollview wont scrollandroid滚动视图不会滚动
【发布时间】:2014-07-15 12:09:00
【问题描述】:

我的滚动视图中有一些线性布局和一些视图,覆盖了整个屏幕。问题是我可以看到我的一个小部件在屏幕下方,它不会向下滚动。这是我的xml文件

<RelativeLayout 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"
    tools:context="${relativePackage}.${activityClass}" >

    <ScrollView
        android:id="@+id/mainScrollView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true"
        android:scrollbars="vertical" >

        <LinearLayout
            android:id="@+id/mainLayout"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <RelativeLayout
                android:id="@+id/topLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <TextView
                    android:id="@+id/textDateTime"
                    android:layout_width="wrap_content"
                    android:text="@string/nullString"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:textColor="#FFCCFF99"
                    android:textSize="25sp" />

                <LinearLayout
                    android:id="@+id/buttonsLayout"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_margin="5dp"
                    android:orientation="horizontal" >

                    <Button
                        android:id="@+id/buttonReset"
                        style="?android:attr/buttonStyleSmall"
                        android:layout_width="130sp"
                        android:layout_height="40dp"
                        android:background="@drawable/button"
                        android:text="@string/buttonResetText"
                        android:textColor="#000000"
                        android:textSize="20sp" />

                    <Button
                        android:id="@+id/buttonFreeze"
                        style="?android:attr/buttonStyleSmall"
                        android:layout_width="130sp"
                        android:layout_height="40sp"
                        android:layout_marginLeft="5dp"
                        android:background="@drawable/button"
                        android:text="@string/buttonFreezeText"
                        android:textColor="#000000"
                        android:textSize="20sp" />
                </LinearLayout>
            </RelativeLayout>

            <ProgressBar
                android:id="@+id/loadBar"
                style="?android:attr/progressBarStyleLarge"
                android:layout_width="118dp"
                android:layout_height="118dp"
                android:layout_gravity="center" />

            <GridView
                android:id="@+id/sensorsGrid"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:background="#FFCCFF99"
                android:gravity="center"
                android:numColumns="6"
                android:stretchMode="none" >
            </GridView>
        </LinearLayout>

    </ScrollView>

</RelativeLayout>

我还在这样的代码中向 mainLayout 添加了一个 GraphView

graph = new LineGraphView(this, "GraphViewDemo");
graph.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 300));
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.mainLayout);
mainLayout.addView(graph);

另一个问题是,如果 GridView 不垂直适合主布局(因为它更大),则 gridView 变得可滚动(我不想要)并且下面的 GraphView 看不到并且我无法滚动归根结底

更新: 当前的xml

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="${relativePackage}.${activityClass}"
    android:id="@+id/mainScrollView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"
    android:scrollbars="vertical" >

        <LinearLayout
            android:id="@+id/mainLayout"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#FF023458"
            android:orientation="vertical" >

            <RelativeLayout
                android:id="@+id/topLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <TextView
                    android:id="@+id/textDateTime"
                    android:layout_width="wrap_content"
                    android:layout_height="40dp"
                    android:layout_margin="5dp"
                    android:text="@string/nullString"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:textColor="#FFCCFF99"
                    android:textSize="25sp" />

                <LinearLayout
                    android:id="@+id/buttonsLayout"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_margin="5dp"
                    android:orientation="horizontal" >

                    <Button
                        android:id="@+id/buttonReset"
                        style="?android:attr/buttonStyleSmall"
                        android:layout_width="130sp"
                        android:layout_height="40dp"
                        android:background="@drawable/button"
                        android:text="@string/buttonResetText"
                        android:textColor="#000000"
                        android:textSize="20sp" />

                    <Button
                        android:id="@+id/buttonFreeze"
                        style="?android:attr/buttonStyleSmall"
                        android:layout_width="130sp"
                        android:layout_height="40sp"
                        android:layout_marginLeft="5dp"
                        android:background="@drawable/button"
                        android:text="@string/buttonFreezeText"
                        android:textColor="#000000"
                        android:textSize="20sp" />
                </LinearLayout>
            </RelativeLayout>

            <ProgressBar
                android:id="@+id/loadBar"
                style="?android:attr/progressBarStyleLarge"
                android:layout_width="118dp"
                android:layout_height="118dp"
                android:layout_gravity="center" />

            <GridView
                android:id="@+id/sensorsGrid"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:background="#FFCCFF99"
                android:gravity="center"
                android:numColumns="6"
                android:stretchMode="none" >
            </GridView>
        </LinearLayout>
</ScrollView>

【问题讨论】:

  • 你为什么不简单地删除父级 RelativeLayout 而让你的 ScrollView 成为父级?
  • 哦,是的,好点,我现在删除了它,但结果还是一样:/

标签: android xml android-layout android-scrollview


【解决方案1】:

您不应该在ScrollView 中添加GridView,因为GridView 本身就是一个可滚动的视图。另一个容器中的可滚动容器将被父容器接管(反之亦然) - 导致无法正确滚动/出现在屏幕上。

提示:您应该将ScrollViewGridView 分开。或者将 GridView 替换为 TableLayout

【讨论】:

  • 谢谢,我会立即尝试,但是没有办法从 gridView 中删除“可滚动性”吗?因为当整个“屏幕”都可以滚动时会更好
  • 从 gridview 中删除滚动功能将使其无用,因为它是一个回收视图。相反,您应该使用 TableLayout,它可以充当 gridview 但没有自己的滚动条。
  • @JuliusShadowAspectFlimmel :这是一个 hack 来覆盖滚动行为stackoverflow.com/questions/6210895/…
  • 我想我得把顶部的 relativeLayout 带回来,然后把 scrollView 和 gridView 放在里面?
  • 您需要在 GridView 中显示相当多的项目吗?如果没有,那么我想坚持使用 TableLayout 就足够了。
【解决方案2】:

将滚动视图标记更改为类似的内容,它会为您工作。

<ScrollView
        android:id="@+id/mainScrollView"
        android:layout_width="fill_parent"
        android:layout_height="500dp"
        android:fillViewport="true"
        android:scrollbars="vertical" >

【讨论】:

  • 谢谢,它有帮助,但你知道,每个设备都有不同的高度,所以滚动视图不会覆盖每个设备上的整个屏幕,对吗?
  • @JuliusShadowAspectFlimmel :您不应该为 ScrollView 使用固定高度
  • 不,它不会像您的应用所针对的最小屏幕那样设计您的 UI 或 XML。然后在较大的设备上,其余部分将保持空白。或使用多个滚动视图。但请记住,每个滚动视图应该只有一个根元素。
  • 你知道...但是如果我有一个空白区域就会出现问题... gridView 可以有很多行,当有空白区域时用户向下滚动它会很烦人行可能在哪里
  • 您还可以通过删除相关布局将滚动视图设为父视图。然后它将适用于所有尺寸的屏幕设备。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多