Android中ScrollView的fillViewport属性不起作用

17

我有一个简单的布局,顶部有一个名称,底部有一个按钮,我希望该按钮位于屏幕底部,或者在我添加更多项目时超出屏幕底部。

因此,我使用了一个包含LinearLayout的ScrollView:

<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:background="@android:color/white"
android:fillViewport="true"
android:focusableInTouchMode="true"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
tools:context=".ItemDetailActivity" >

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

    <!-- Name -->

    <RelativeLayout
        android:id="@+id/top_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top" >

        <TextView
            android:id="@+id/name_label"
            style="@style/Header_Label_TextView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/name" />

        <TextView
            android:id="@+id/name_value"
            style="@style/Value_Label_TextView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/name_label" />
    </RelativeLayout>

    <!-- button -->

    <RelativeLayout
        android:id="@+id/ButtonLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom" >

        <Button
            android:id="@+id/add_button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10sp"
            android:onClick="editItem"
            android:text="@string/button_edit" />
    </RelativeLayout>

    <requestFocus />
</LinearLayout>

这就是我得到的:

布局XML的结果

我该如何使按钮出现在屏幕底部或屏幕之外?在网上搜索后,大多数答案都是设置 `android:fillViewport="true"`,但这并没有帮助解决问题。我做错了什么?

4个回答

6
将您的按钮布局更改为以下内容:
 <RelativeLayout
    android:id="@+id/ButtonLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="bottom" >

    <Button
        android:id="@+id/add_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10sp"
        android:onClick="editItem"
        android:layout_alignParentBottom="true"
        android:text="@string/button_edit" />
</RelativeLayout>

是的,这个有效。我之前将这个属性应用到了相对布局上,但它并没有起作用。现在我想我明白发生了什么了! - rgamber

3

这应该可以工作 -

  1. 将RelativeLayout作为ScrollView的子元素。
  2. 设置 layout_alignParentBottom = true 来对按钮进行定位。

示例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:background="@android:color/white"
android:fillViewport="true"
android:focusableInTouchMode="true"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
tools:context=".ItemDetailActivity" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- Name -->

    <RelativeLayout
        android:id="@+id/top_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top" >

        <TextView
            android:id="@+id/name_label"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Name" />

        <TextView
            android:id="@+id/name_value"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/name_label" />
    </RelativeLayout>

    <!-- button -->

    <RelativeLayout
        android:id="@+id/ButtonLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <Button
            android:id="@+id/add_button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10sp"
            android:onClick="editItem"
            android:text="Edit" />
    </RelativeLayout>
</RelativeLayout>

</ScrollView>

如果在EditView焦点上出现虚拟键盘,由于您指示它对齐到底部,因此底部的布局也会上移。因此,只需在您的布局中放置一个EditView,并在分辨率较小的设备上进行测试即可。重新调整大小以适应键盘的onConfigChange也不起作用。您可以在相对于此视图的倒数第二个项目下方添加间隔器视图。 - Abhinav Saxena

1
在滚动视图中,使用RelativeLayout而不是LinearLayout,并设置底部视图属性。
android:layout_alignParentBottom="true"

0
如果您正在使用LinearLayout(比RelativeLayout更高效),则应该像这样设置android:gravity="bottom"android:layout_height="match_parent"
<LinearLayout
    android:id="@+id/ButtonLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="bottom" >

    <Button
        android:id="@+id/add_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10sp"
        android:onClick="editItem"
        android:text="@string/button_edit" />
</LinearLayout>

参考资料:https://demonuts.com/android-fillviewport/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接