0

在输入文本时在相对布局(包含在滚动视图中)中添加 EditText 视图时,它会将用户带回活动的顶部,(捕捉到屏幕顶部)使用户无法看到他们正在输入的内容。

复制问题

这是该问题的最小再现:

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

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

        <ImageView
            android:layout_width="100dp"
            android:layout_height="1000dp"
            android:src="@color/purple_500"/>
        <EditText
            android:layout_width="400dp"
            android:layout_height="100dp"
            android:translationY="800dp"/>

    </RelativeLayout>

</ScrollView>

到目前为止我尝试过的

到目前为止,我已经尝试添加 <activity android:windowSoftInputMode="stateVisible|adjustResize" .>到 android 清单中,但它没有任何区别并填充 EditText 虽然我找不到其他发生这种情况的情况。

添加wrap_contentandroid:layout_height="",未解决的问题

编辑

我需要在相对布局中包含线性布局中的编辑文本,如此处所示-https://stackoverflow.com/questions/40316454/edittext-moves-textview-out-of-the-screen-when-they -keyboard-is-opening

4

1 回答 1

0

为了避免在 EditText 上写入期间自动滚动到顶部效果,您可以创建 ScrollView 的子类并将 0 返回到受保护的方法:computeScrollDeltaToGetChildRectOnScreen(Rect rect)

computeScrollDeltaToGetChildRectOnScreen

计算在 Y 方向上滚动的量,以便在屏幕上完全显示一个矩形(或者,如果比屏幕高,至少是它的第一个屏幕大小块)。

创建 ScrollView 的子类并在该方法上返回 0,如下所示:

public class CustomScrollView extends ScrollView {

    public CustomScrollView(Context context) {
        super(context);
    }

    public CustomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected int computeScrollDeltaToGetChildRectOnScreen(Rect rect) {
        return 0;
    }
}

xml 用法:

<?xml version="1.0" encoding="utf-8"?>
<my.package.name.CustomScrollView
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity"
    xmlns:android="http://schemas.android.com/apk/res/android">

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

        <ImageView
            android:layout_width="100dp"
            android:layout_height="1000dp"
            android:src="@color/purple_500"/>
        <EditText
            android:layout_width="400dp"
            android:layout_height="100dp"
            android:translationY="800dp"/>

    </RelativeLayout>

</my.package.name.CustomScrollView>
于 2021-07-13T08:01:00.983 回答