【问题标题】:(Android) ScrollView won't scroll all the way to the bottom of my LinearLayout(Android) ScrollView 不会一直滚动到我的 LinearLayout 的底部
【发布时间】:2013-06-27 07:53:09
【问题描述】:

所以我有一个带有 LinearLayout 的 ScrollView。 似乎当我尝试滚动到线性布局的底部时,底部~5 倾角被切断(即底部边距) 我认为这可能与我的线性布局的 5dip 边距有关?

这里是activity_create_account.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="@drawable/grey"
android:orientation="vertical"
android:padding="0dp"
tools:context=".Login" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="100"
android:orientation="vertical">

<!-- BEGIN HEADER -->
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="@drawable/titlebar"
    android:orientation="horizontal"
    android:padding="8dip" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="0dp"
        android:text="create account"
        android:textColor="#FFFFFF"
        android:textSize="32sp"
        android:textStyle="bold"
        android:typeface="sans" />
</LinearLayout>
<!-- END HEADER -->

<!-- BEGIN BODY -->

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="top"
    android:orientation="vertical">

<LinearLayout
    android:id="@+id/innerLinearLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dip"
    android:background="@drawable/rounded_white"
    android:orientation="vertical"
    android:padding="5dip" >

    <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:ems="10"
            android:id="@+id/etCreateEmail"
            android:hint="Email"
            android:layout_weight="1"
            android:paddingTop="8dip"
            android:paddingBottom="8dip"
            android:paddingRight="8dip"
            android:paddingLeft="8dip"
            android:layout_marginBottom="4dip"
            android:layout_marginTop="5dip"/>
    <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:ems="10"
            android:id="@+id/etCreateEmail"
            android:hint="Email"
            android:layout_weight="1"
            android:paddingTop="8dip"
            android:paddingBottom="8dip"
            android:paddingRight="8dip"
            android:paddingLeft="8dip"
            android:layout_marginBottom="4dip"
            android:layout_marginTop="5dip"/>
    <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:ems="10"
            android:id="@+id/etCreateEmail"
            android:hint="Email"
            android:layout_weight="1"
            android:paddingTop="8dip"
            android:paddingBottom="8dip"
            android:paddingRight="8dip"
            android:paddingLeft="8dip"
            android:layout_marginBottom="4dip"
            android:layout_marginTop="5dip"/>
    <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:ems="10"
            android:id="@+id/etCreateEmail"
            android:hint="Email"
            android:layout_weight="1"
            android:paddingTop="8dip"
            android:paddingBottom="8dip"
            android:paddingRight="8dip"
            android:paddingLeft="8dip"
            android:layout_marginBottom="4dip"
            android:layout_marginTop="5dip"/>
    <EditText
        android:id="@+id/etChooseUsername"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dip"
        android:layout_weight="1"
        android:ems="10"
        android:hint="Choose a username"
        android:inputType="text"
        android:paddingBottom="8dip"
        android:paddingLeft="8dip"
        android:paddingRight="8dip"
        android:paddingTop="8dip" />
    <EditText
        android:id="@+id/etChoosePassword"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dip"
        android:layout_weight="1"
        android:ems="10"
        android:hint="Choose a password"
        android:inputType="textPassword"
        android:paddingBottom="8dip"
        android:paddingLeft="8dip"
        android:paddingRight="8dip"
        android:paddingTop="8dip" />
    <EditText
        android:id="@+id/etRetypePassword"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dip"
        android:layout_weight="1"
        android:ems="10"
        android:hint="Re-type password"
        android:inputType="textPassword"
        android:paddingBottom="8dip"
        android:paddingLeft="8dip"
        android:paddingRight="8dip"
        android:paddingTop="8dip" />

    <Button
        android:id="@+id/bCreateAccountConfirm"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_weight="1"
        android:background="@drawable/button_selector"
        android:padding="0dip"
        android:text="Create Account"
        android:textColor="#ffffff"
        android:textStyle="bold"
        android:typeface="sans" />
</LinearLayout>
</ScrollView>
<!-- END BODY -->

这是我尝试一直向下滚动时的样子(按钮下方应该有一个细长的白色部分,然后是一个细长的灰色边缘部分)

【问题讨论】:

  • 不要将android:layout_margin="10dip" 放在android:id="@+id/innerLinearLayout" 上,而是将android:padding="10dip" 放在ScrollView
  • @bogdan 当我在 ScrollView 上添加填充时,它仍然会切断我内部布局的底部(并且无论滚动到哪里都会在屏幕底部留下难看的 10dip 宽的灰色部分)
  • 我可能读错了这个问题,忘记了我所说的关于滚动视图的 10dp 填充的更改,sry。您希望“创建帐户”按钮在底部有一个小边距,所以在 android:id="@+id/bCreateAccountConfirm" 上放置一个 android:layout_marginBottom="10dp"
  • 在我的应用程序中,我在 LinearLayout 中设置了 margintop 并且存在同样的问题。然后我设置了paddingTop,就可以了。我认为这是一个错误,因为 margintop 使布局完全向下移动!

标签: android android-scrollview


【解决方案1】:

我知道这个问题有点老了 - 但为了那些通过谷歌找到这个问题的人,我会试着解释一下。

似乎通用 ScrollView 对象在与其他兄弟姐妹的布局中使用时效果不佳。因此,谷歌创建了一个NestedScrollView。从文档中,

NestedScrollView 与 ScrollView 类似,但它支持在新旧版本的 Android 上同时充当嵌套滚动父项和子项。

我遇到了与这个问题提出的问题类似的问题,使用 NestedScrollView 消除了原始 ScrollView 底部的截断。

【讨论】:

  • 谢谢!为我节省了很多时间,而且工作起来就像一个魅力
  • 很遗憾,这让我的应用程序崩溃了。不知道为什么。有什么想法吗?
【解决方案2】:

在您的 ScrollView 中添加 padding_bottom 到大约 10dp。它会起作用的。

否则,Horizo​​ntalView 下方的视图可能会覆盖在此水平视图上方。 那样的话

  1. 给 Horizo​​ntalView 添加 id id="@+id/horizontalView"
  2. 在水平视图下方的视图中添加below="@+id/horizontalView"

【讨论】:

  • 非常感谢 Naresh :)
  • “Horizo​​ntalView”是什么意思?
  • 我想它可能已更改为“Horizo​​ntalScrollView”
  • 为什么需要添加填充?我理解它为什么起作用,但是为什么滚动视图首先被破坏了?编辑:我通过将滚动视图的底部限制到父级的底部来解决了我的这个问题的实例。所以我猜这个视图比父视图大
【解决方案3】:

您可以将 ma​​rginBottom 设置为您的滚动视图

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="top"
    android:layout_marginBottom="10dp"
    android:orientation="vertical">

【讨论】:

  • 我遇到了类似的问题,这是由于滚动视图内 textview 的边距所致。从文本视图中删除边距,并将其添加到滚动视图意味着所有文本都可见并且滚动按预期工作。在我的情况下,我有 20dp 的边距,所以缺少一整行,所以我将它从 textview 的底部边距中删除,并添加到滚动中。
  • 在我的例子中,原因是 TableLayout 使用了高度为 2dp 的分隔线。我有 30 行,所以我必须添加 60dp 的 marginBottom 并且它一直滚动到最后,包括最后一行
  • 这有点小技巧,但它确实有效,而且与找出 Android 渲染引擎的“巫毒行为”相比,它是最简单的解决方案。
【解决方案4】:
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"

真的为我解决了这个问题,不需要 NestedView 或填充。 保持 ScrollView 的直接子视图的 layout_height 为“wrapped_content”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-14
    相关资源
    最近更新 更多