【问题标题】:Scroll View not showing all the views in android滚动视图未显示android中的所有视图
【发布时间】:2013-11-26 12:02:25
【问题描述】:

我无法在 ScrollView 下看到最顶部的视图。 我已将滚动视图放在相对布局下。 ScrollView 有一个子布局,即具有一些按钮系列的线性布局。 问题是我无法看到 ScrollView 中出现的最顶部的视图。

下面是布局xml代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#217d27"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:onClick="backPressed"
            android:text="Back"
            android:textColor="#000" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:gravity="center"
            android:text="Heading Text"
            android:textColor="#fff"
            android:textSize="20sp" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="10dp"
            android:onClick="homeButtonClicked"
            android:text="Home"
            android:textColor="#000" />
    </RelativeLayout>

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:fadingEdge="none"
        android:fillViewport="true" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="#fff"
            android:orientation="vertical" >

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Button 1"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 2"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 3"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 4"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 5"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 6"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 7"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 8"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 9"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 10"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 11"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 12"
                android:textColor="@color/black" />
        </LinearLayout>
    </ScrollView>

</LinearLayout>

请参阅屏幕截图以供参考。

【问题讨论】:

  • 使用相对布局而不是线性布局。
  • Nope 相对布局也不起作用。我得到了同样的结果。
  • 你的代码是 99% 完美的,只需删除 1 行只看我的答案我更新你的代码老兄,你总是欢迎 :)
  • 谢谢老兄。它现在可以工作了。
  • 我不应该在线性布局中使用这个 android:layout_gravity="center"。

标签: android android-layout android-scrollview


【解决方案1】:

只需在线性布局中删除此行,它是滚动视图的直接子级 android:layout_gravity="center" 试试这个更新的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#217d27"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:onClick="backPressed"
            android:text="Back"
            android:textColor="#000" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:gravity="center"
            android:text="Heading Text"
            android:textColor="#fff"
            android:textSize="20sp" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="10dp"
            android:onClick="homeButtonClicked"
            android:text="Home"
            android:textColor="#000" />
    </RelativeLayout>

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:fadingEdge="none"
        android:fillViewport="true" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#fff"
            android:orientation="vertical" >

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Button 1"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 2"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 3"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 4"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 5"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 6"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 7"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 8"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 9"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 10"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 11"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 12"
                android:textColor="@color/black" />
        </LinearLayout>
    </ScrollView>

</LinearLayout>

【讨论】:

    【解决方案2】:

    只需更改ScrollView 内的LinearLayout

    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top|center"
            android:background="#fff"
            android:orientation="vertical" >
    

    提供android:layout_gravity="top|center" 而不是android:layout_gravity="center"

    希望对你有帮助。

    【讨论】:

    • 非常感谢 Anuj。
    • 天啊.. 太棒了:D
    【解决方案3】:

    这个值帮助我解决了一个类似的问题: 在 ScrollView 上设置 android:fillViewport="true" 并在 ScrollView 子项上使用 android:gravity 而不是 android:layout_gravity,在我的例子中是一个所有权重都已清除的 LinearLayout。

    【讨论】:

      【解决方案4】:

      尝试将父布局更改为线性。它可能会起作用。不知道如何以及为什么,但对我来说,它在类似的情况下工作。试试看吧。

      【讨论】:

        【解决方案5】:

        提供 50dp 的顶部内边距

         <LinearLayout
                    android:id="@+id/linearLayout"
                    android:paddingTop="50dp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" />
        

        【讨论】:

        • 临时解决方案,如“Jugaad”
        【解决方案6】:

        在线性布局中使用 android:layout_gravity="center"

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-01-16
          • 1970-01-01
          • 2014-02-16
          • 1970-01-01
          • 2016-03-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多