Android ScrollView fillViewport无法正常工作

时间:2013-09-13 07:26:17

标签: android android-layout android-linearlayout android-scrollview android-relativelayout

我有一个简单的布局,顶部有一个名字,还有一个我希望位于屏幕底部的按钮,或者我想要添加更多项目的按钮。

所以我使用带有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>

这就是我得到的:

Result of the layout XML

如何进行操作以使按钮出现在屏幕底部或更远处。在线搜索,大多数答案是设置`android:fillViewport =“true”,但这没有帮助。我做错了什么?

4 个答案:

答案 0 :(得分:5)

像这样更改按钮布局:

 <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>

答案 1 :(得分:3)

这应该有效 -

  1. 将相对布局用作滚动视图的子项。
  2. 为按钮设置layout_alignParentBottom = true
  3. 示例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>
    

答案 2 :(得分:0)

在滚动视图中,用户RelativeLayout而不是LinearLayout并设置底部视图属性

android:align_parentBottom"="true"

答案 3 :(得分: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/

相关问题