【问题标题】:ScrollView doesn't show contentScrollView 不显示内容
【发布时间】:2013-04-22 20:54:05
【问题描述】:

谁能解释一下为什么滚动视图不显示他的子元素? 我在线性布局垂直中有很多文本视图,线性布局在滚动视图内......

但我没有看到任何滚动视图,也没有 textviews..

这是 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" >

    <TextView
        android:id="@+id/description"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/scrollViewCheck"
        android:gravity="center"
        android:padding="10dip"
        android:text="Pick which items you want to count"
        android:textAppearance="?android:attr/textAppearanceMedium" >
    </TextView>

    <ScrollView
        android:id="@id/scrollViewCheck"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bottomSettings"
        android:scrollbarFadeDuration="0" >

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Test"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Test"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Test"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Test"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Test"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Test"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Test"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Test"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:text="Test"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:text="Test"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </LinearLayout>
    </ScrollView>

    <RelativeLayout
        android:id="@id/bottomSettings"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/timeButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Choose Count Time" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/timeButton"
            android:orientation="horizontal" >

            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Back" />

            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Start" />
        </LinearLayout>
    </RelativeLayout>

</RelativeLayout>

提前致谢;)

【问题讨论】:

    标签: android android-layout textview scrollview


    【解决方案1】:

    视图的问题与底部的RelativeLayout有关。这应该可以解决您的问题。将底部的 RelativeLayout 替换为:

     <LinearLayout
            android:id="@+id/bottomSettings"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_alignParentBottom="true">
    
        <Button
                android:id="@+id/timeButton"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Choose Count Time" />
    
        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/timeButton"
                android:orientation="horizontal" >
    
            <Button
                    android:layout_width="0dip"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Back" />
    
            <Button
                    android:layout_width="0dip"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Start" />
    
        </LinearLayout>
    
    </LinearLayout>
    

    【讨论】:

    • 这解决了整个问题,但老兄有时我会卡在很多布局上……有很多小故障。我的意思是,布局应该更容易制作,有很多小事情会改变整个内容。有什么方法可以更快更简单地设计它? adt活动设计模式一点都不好..谢谢!
    • @DarkLink 我通常做设计的方式是从一些非常简单的东西开始,然后一次添加一个元素。这样我就知道问题出现在哪里了。
    • 是的,这正是我所期待的,很多试验和错误,就像 David Wasser 说的 :) 谢谢!
    【解决方案2】:

    您正在使用 RelativeLayout 作为根布局,并且缺少像 android:layout_belowandroid:layout_above 这样的布局说明。

    这就是为什么元素会相互重叠而您看不到您的ScrollView

    将您的根布局更改为具有android:orientation="vertical" 属性的LinearLayout,您将看到所有小部件。

    【讨论】:

      【解决方案3】:

      我认为您不能将ScrollView 高度定义为“wrap_content”。由于内容远远大于屏幕上的大小。通常您需要将ScrollView 的高度定义为某个固定值。如果您希望 ScrollView 尽可能多地占据屏幕,同时仍然拥有页眉和页脚视图,您可以试试这个:

         android:layout_height="1dp"
         android:layout_weight="1"
      

      这基本上会将屏幕上的所有剩余空间分配给ScrollView。我这里没有机器来测试这个,但是试一试,让我知道它是如何工作的。

      【讨论】:

      • 我测试了这个解决方案,它也有效!谢谢 ;) 顺便说一句:“有时我会卡在很多布局上……有很多小故障。我的意思是,布局应该更容易制作,有很多小事情会改变整个内容..有什么方法可以设计得更快更简单?adt活动设计模式一点都不好..谢谢!”
      • 我对你没有任何建议。我不使用 ADT 的东西,我只是编辑 XML 并再试一次。我花了很长时间才开始理解不同的布局参数是如何相互作用的。不幸的是,有许多不同的方法可以实现结果,并且不同布局参数之间存在复杂的依赖关系和交互。 Android 的设计者/开发者从来没有觉得有必要详细记录所有这些依赖关系,所以你和我必须通过反复试验来弄清楚。当然,StackOverflow 用户帮了大忙 :-)
      • 他们绝对是!如果没有像你这样的成员,我会很迷茫,不知道为什么事情不起作用,再次感谢!我明白感谢您的解释,希望他们能发布涵盖所有小细节的优质文档:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-15
      • 2017-08-20
      相关资源
      最近更新 更多