android - 添加 ScrollView 时布局向上移动

标签 android android-layout android-scrollview

我试图使我的 Activity 可滚动,所以我将相对布局包含在 ScrollView 中。但它所做的是将整个布局向上移动。有人可以帮助解决问题吗?

这是 Activity 的 xml 文件;

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="nmss.example.com.coach.UserProfile">

<ImageView
    android:id="@+id/iv_dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/tv_statusd"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="37dp"
    android:layout_marginStart="17dp"
    app:srcCompat="@mipmap/ic_launcher" />

<EditText
    android:id="@+id/tv_statusd"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_above="@+id/et_age"
    android:layout_alignParentEnd="true"
    android:layout_marginBottom="19dp"
    android:layout_marginEnd="11dp"
    android:layout_marginRight="11dp"
    android:ems="10"
    android:hint="Status"
    android:imeOptions="actionNext"
    android:inputType="textPersonName"
    android:maxLines="1"
    android:maxLength="80"/>

<EditText
    android:id="@+id/et_age"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/et_height"
    android:layout_alignStart="@+id/tv_statusd"
    android:layout_marginBottom="28dp"
    android:ems="10"
    android:hint="Age"
    android:imeOptions="actionNext"
    android:inputType="number" />

<EditText
    android:id="@+id/et_height"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/et_weight"
    android:layout_alignStart="@+id/et_age"
    android:layout_marginBottom="23dp"
    android:ems="10"
    android:hint="Height"
    android:imeOptions="actionNext"
    android:inputType="numberDecimal" />

<EditText
    android:id="@+id/et_weight"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/et_exp"
    android:layout_alignStart="@+id/et_height"
    android:layout_marginBottom="22dp"
    android:ems="10"
    android:hint="Weight"
    android:imeOptions="actionNext"
    android:inputType="numberDecimal" />

<EditText
    android:id="@+id/et_exp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/et_bio"
    android:layout_alignStart="@+id/et_weight"
    android:layout_marginBottom="17dp"
    android:ems="10"
    android:hint="Experiences"
    android:imeOptions="actionNext"
    android:inputType="textMultiLine" />

<EditText
    android:id="@+id/et_bio"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/btn_done"
    android:layout_alignStart="@+id/et_exp"
    android:layout_marginBottom="33dp"
    android:ems="10"
    android:hint="Bio"
    android:imeOptions="actionDone"
    android:inputType="textMultiLine" />

<Button
    android:id="@+id/btn_done"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="35dp"
    android:text="Done" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="11dp"
    android:text="PROFILE"
    android:textAllCaps="true"
    android:textSize="24sp"
    android:textStyle="bold" />

</RelativeLayout>
</ScrollView>

这是添加scrollview之前的布局预览

enter image description here

这是添加scrollview后的布局预览

enter image description here

最佳答案

将此属性添加到 Scroll View。修改您的 Scroll View,如下所示。无需替换任何其他布局

此属性 android:fillViewport="true"android:scrollbars="vertical" 将执行此操作。

<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:fillViewport="true"
    android:scrollbars="vertical">

这会正确显示元素。但是当我点击编辑文本时,整个布局向上移动,一切都变得杂乱无章。怎么办?

将此行添加到您的 Activity 并尝试。 Adjust ResizeAdjust Pan 将执行此操作。

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

尝试在 manifest activity 中定义它。

<activity
    android:name=".TestActivity"
    android:windowSoftInputMode="stateHidden|adjustResize">

使用这个编辑过的 XML。做了一些修改。

   <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:scrollbars="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:context="nmss.example.com.coach.UserProfile">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="11dp"
            android:text="PROFILE"
            android:textAllCaps="true"
            android:textSize="24sp"
            android:textStyle="bold" />

        <ImageView
            android:id="@+id/iv_dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/tv_statusd"
            android:layout_alignParentStart="true"
            android:layout_marginLeft="37dp"
            android:layout_marginStart="17dp"
            app:srcCompat="@mipmap/ic_launcher"
            tools:ignore="RtlCompat" />

        <EditText
            android:id="@+id/tv_statusd"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_below="@+id/textView2"
            android:layout_marginEnd="11dp"
            android:layout_marginRight="11dp"
            android:layout_marginTop="30dp"
            android:ems="10"
            android:hint="Status"
            android:imeOptions="actionNext"
            android:inputType="textPersonName"
            android:maxLength="80"
            android:maxLines="1"
            tools:ignore="RtlCompat" />

        <EditText
            android:id="@+id/et_age"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignStart="@+id/tv_statusd"
            android:layout_below="@+id/tv_statusd"
            android:layout_marginTop="10dp"
            android:ems="10"
            android:hint="Age"
            android:imeOptions="actionNext"
            android:inputType="number"
            android:maxLines="1"
            tools:ignore="RtlCompat" />

        <EditText
            android:id="@+id/et_height"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignStart="@+id/et_age"
            android:layout_below="@+id/et_age"
            android:layout_marginTop="10dp"
            android:ems="10"
            android:hint="Height"
            android:imeOptions="actionNext"
            android:inputType="numberDecimal"
            android:maxLines="1"
            tools:ignore="RtlCompat" />

        <EditText
            android:id="@+id/et_weight"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignStart="@+id/et_height"
            android:layout_below="@+id/et_height"
            android:layout_marginTop="10dp"
            android:ems="10"
            android:hint="Weight"
            android:imeOptions="actionNext"
            android:inputType="numberDecimal"
            android:maxLines="1"
            tools:ignore="RtlCompat" />

        <EditText
            android:id="@+id/et_exp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignStart="@+id/et_weight"
            android:layout_below="@+id/et_weight"
            android:layout_marginTop="10dp"
            android:ems="10"
            android:hint="Experiences"
            android:imeOptions="actionNext"
            android:inputType="text"
            android:maxLines="3"
            tools:ignore="RtlCompat" />

        <EditText
            android:id="@+id/et_bio"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignStart="@+id/et_exp"
            android:layout_below="@+id/et_exp"
            android:layout_marginTop="10dp"
            android:ems="10"
            android:hint="Bio"
            android:imeOptions="actionDone"
            android:inputType="text"
            android:maxLines="3"
            tools:ignore="RtlCompat" />

        <Button
            android:id="@+id/btn_done"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/et_bio"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="15dp"
            android:text="Done" />


    </RelativeLayout>
</ScrollView>

关于android - 添加 ScrollView 时布局向上移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44105371/

相关文章:

android - 如何使RecyclerView在NestedScrollView中进行回收?

java - 访问安全系统设置

android - 检测我的应用程序是否已卸载

android - 使用 TabLayout 时如何更改选项卡背景颜色?

android - 编辑 Canvas 部分

android-layout - 使用 9-patch 图像进行应用程序开发

android - 在 ScrollView 中使用表格布局

java - 如何让 ScrollView 自动滚动到底部?

android - 为什么 Android 4.2 会尝试从 drawable-xhdpi-v4 获取资源

android - 如何删除 Gradle 冲突