You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio页面无法滚动到底部问题修复咨询

Android页面无法滚动到底部,底部「Зберегти」按钮仅显示一半的修复方案

问题描述

开发时遇到页面无法滚动到底部,底部的「Зберегти」按钮只显示一半,尝试调整margin、padding、布局高度均无效。

原布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".UploadActivity">
    <ImageButton
        android:id="@+id/uploadButton_close"
        android:layout_width="25dp"
        android:layout_height="26dp"
        android:layout_marginStart="20dp"
        android:layout_marginBottom="84dp"
        android:layout_marginTop="10dp"
        android:background="@android:color/transparent"
        android:scaleType="fitCenter"
        app:srcCompat="@drawable/close_icon" />
            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                >
                <androidx.cardview.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="50dp"
                    android:layout_marginEnd="20dp"
                    android:layout_marginStart="20dp"
                    app:cardCornerRadius="30dp"
                    app:cardElevation="20dp">
                        <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical"
                        android:layout_gravity="center_horizontal"
                            android:padding="20dp"
                        android:background="@drawable/lavender_border">
                        <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Завантажте дані"
                        android:textSize="30sp"
                        android:textAlignment="center"
                        android:textColor="@color/purple" />
                        <ImageView
                        android:id="@+id/uploadImage"
                        android:layout_width="match_parent"
                        android:layout_height="200dp"
                        android:scaleType="fitCenter"
                        android:src="@drawable/baseline_image_search_24"
                        android:layout_marginTop="10dp"
                        />
                        <EditText
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
                        android:id="@+id/uploadTitle"
                        android:textSize="16sp"
                        android:textStyle="bold"
                        android:background="@drawable/lavender_border"
                        android:hint="Введіть заголовок"
                        android:layout_marginTop="20dp"
                        android:padding="16dp"
                        android:layout_gravity="start|center_vertical"
                        android:textColor="@color/purple"/>
                        <EditText
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
                        android:id="@+id/uploadCaption"
                        android:textSize="14sp"
                        android:background="@drawable/lavender_border"
                        android:hint="Введіть короткий опис"
                        android:layout_marginTop="20dp"
                        android:padding="16dp"
                        android:layout_gravity="start|center_vertical"
                        android:textColor="@color/purple"/>
                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/uploadText"
                        android:background="@drawable/lavender_border"
                        android:hint="Введіть текст"
                        android:textSize="14sp"
                        android:layout_marginTop="20dp"
                        android:padding="16dp"
                        android:layout_gravity="start|center_vertical"
                        android:textColor="@color/purple"
                        android:inputType="textMultiLine"
                        android:minLines="12"
                        android:gravity="top"
                        />
                        <EditText
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
                        android:id="@+id/uploadLink"
                        android:textSize="14sp"
                        android:background="@drawable/lavender_border"
                        android:hint="Посилання"
                        android:layout_marginTop="20dp"
                        android:padding="16dp"
                        android:layout_gravity="start|center_vertical"
                        android:textColor="@color/purple"/>
                        <AutoCompleteTextView
                            android:id="@+id/uploadTheme"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="start|center_vertical"
                            android:layout_marginTop="20dp"
                            android:background="@drawable/lavender_border"
                            android:hint="Оберіть тему"
                            android:inputType="none"
                            android:padding="16dp"
                            android:textColor="@color/purple"
                            android:textSize="14sp"
                            tools:ignore="SpeakableTextPresentCheck"
                            android:drawableEnd="@drawable/baseline_arrow_drop_down_24"/>
                        <Button
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
                         android:text="Зберегти"
                        android:id="@+id/uploadButton_save"
                        android:textSize="18sp"
                        android:layout_marginTop="10dp"
                            />

        </LinearLayout>

    </androidx.cardview.widget.CardView>

</ScrollView>
</RelativeLayout>

问题根源

  1. ScrollView使用wrap_content高度,且在RelativeLayout中未约束底部位置,导致ScrollView底部超出屏幕范围,无法滚动到最底端。
  2. 顶部关闭按钮设置了不合理的layout_marginBottom,干扰了ScrollView的布局定位。
  3. CardView缺少底部边距,导致内部按钮贴近ScrollView底部,容易被截断。

修复步骤

  1. 调整ScrollView的布局约束与高度:

    • 将ScrollView的android:layout_height改为match_parent
    • 添加android:layout_below="@id/uploadButton_close",让ScrollView位于关闭按钮下方
    • 添加android:layout_marginBottom="20dp",给ScrollView底部留足够空间
  2. 修正关闭按钮的布局:

    • 移除关闭按钮的android:layout_marginBottom="84dp"
    • 添加android:layout_alignParentTop="true",确保按钮固定在页面顶部
  3. 给CardView添加底部边距:

    • 在CardView中添加android:layout_marginBottom="20dp",避免内部内容贴近ScrollView底部

修改后的完整代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".UploadActivity">
    <ImageButton
        android:id="@+id/uploadButton_close"
        android:layout_width="25dp"
        android:layout_height="26dp"
        android:layout_alignParentTop="true"
        android:layout_marginStart="20dp"
        android:layout_marginTop="10dp"
        android:background="@android:color/transparent"
        android:scaleType="fitCenter"
        app:srcCompat="@drawable/close_icon" />
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/uploadButton_close"
        android:layout_marginBottom="20dp"
        android:orientation="vertical">
        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:layout_marginStart="20dp"
            android:layout_marginEnd="20dp"
            android:layout_marginBottom="20dp"
            app:cardCornerRadius="30dp"
            app:cardElevation="20dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:layout_gravity="center_horizontal"
                android:padding="20dp"
                android:background="@drawable/lavender_border">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Завантажте дані"
                    android:textSize="30sp"
                    android:textAlignment="center"
                    android:textColor="@color/purple" />
                <ImageView
                    android:id="@+id/uploadImage"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:scaleType="fitCenter"
                    android:src="@drawable/baseline_image_search_24"
                    android:layout_marginTop="10dp" />
                <EditText
                    android:id="@+id/uploadTitle"
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:textSize="16sp"
                    android:textStyle="bold"
                    android:background="@drawable/lavender_border"
                    android:hint="Введіть заголовок"
                    android:layout_marginTop="20dp"
                    android:padding="16dp"
                    android:layout_gravity="start|center_vertical"
                    android:textColor="@color/purple"/>
                <EditText
                    android:id="@+id/uploadCaption"
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:textSize="14sp"
                    android:background="@drawable/lavender_border"
                    android:hint="Введіть короткий опис"
                    android:layout_marginTop="20dp"
                    android:padding="16dp"
                    android:layout_gravity="start|center_vertical"
                    android:textColor="@color/purple"/>
                <EditText
                    android:id="@+id/uploadText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/lavender_border"
                    android:hint="Введіть текст"
                    android:textSize="14sp"
                    android:layout_marginTop="20dp"
                    android:padding="16dp"
                    android:layout_gravity="start|center_vertical"
                    android:textColor="@color/purple"
                    android:inputType="textMultiLine"
                    android:minLines="12"
                    android:gravity="top" />
                <EditText
                    android:id="@+id/uploadLink"
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:textSize="14sp"
                    android:background="@drawable/lavender_border"
                    android:hint="Посилання"
                    android:layout_marginTop="20dp"
                    android:padding="16dp"
                    android:layout_gravity="start|center_vertical"
                    android:textColor="@color/purple"/>
                <AutoCompleteTextView
                    android:id="@+id/uploadTheme"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="start|center_vertical"
                    android:layout_marginTop="20dp"
                    android:background="@drawable/lavender_border"
                    android:hint="Оберіть тему"
                    android:inputType="none"
                    android:padding="16dp"
                    android:textColor="@color/purple"
                    android:textSize="14sp"
                    tools:ignore="SpeakableTextPresentCheck"
                    android:drawableEnd="@drawable/baseline_arrow_drop_down_24"/>
                <Button
                    android:id="@+id/uploadButton_save"
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:text="Зберегти"
                    android:textSize="18sp"
                    android:layout_marginTop="10dp" />
            </LinearLayout>
        </androidx.cardview.widget.CardView>
    </ScrollView>
</RelativeLayout>

内容的提问来源于stack exchange,提问作者Валерия

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 00:45:56