Android ScrollView fillViewport 不工作

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

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

所以我使用 ScrollView 和 LinearLayout 如下:

<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",但这并没有帮助。我做错了什么?

最佳答案

像这样改变你的按钮布局:

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

关于Android ScrollView fillViewport 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18780619/

相关文章:

android - PhoneGap + jqueryMobile 文本转语音

android - 缩小屏幕上的对话框

android - 在使用 bringToFront() 方法时遇到问题

java - Android 进度条大小调整

Android设备使用 "Shark for Root"抓包却收到 "Empty file opened"错误

java - 接收 JSON 并将其转换为对象的最佳实践

android - 无法在 Android TV 上创建的 channel 上发布通知

java - Android:设置按钮高度与宽度相同

android - 如何以编程方式设置底部边距?

android - 线性布局留空行