ScrollView在键盘弹出时忽略了padding。

6
假设我有这样一个简单的布局: https://imgur.com/k6zuh3f
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="80dp"
            android:clipToPadding="false">

        <LinearLayout
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="16dp">

            <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/ic_launcher" />

            ...


            <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Eighth" />

        </LinearLayout>

    </ScrollView>

    <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_margin="16dp"
            android:text="Button" />

</RelativeLayout>

请注意,在设置clipToPadding=falseScrollView中有paddingBottom。这是为了使Button看起来像是悬浮在滚动视图内容上。ScrollView padding被用来在内容下方留出空间,以便使最后一个子元素可用。
如果最后一个子元素是EditText,那么我希望ScrollView在滚动时可以使EditText可见,超过软键盘。但实际结果是:https://imgur.com/GwOtLIq 使用layout_marginBottom而不是paddingBottom可以实现预期行为,但在这种情况下,我无法在Button后面看到我的内容。截图:https://imgur.com/oSC24qV 是否有办法使ScrollView在避免键盘时尊重其填充?更新:完整的XML代码在此处:https://pastebin.com/P8n0aZ2i

快速问题,打开软键盘后你能否向上滚动?因为我使用顶部的ConstraintLayout,你的相同xml对我有效。打开软键盘后,我可以向上滚动,显示在按钮上方的最后一个EditText - Syed Ahmed Jamil
你的项目中最后一个 EditText 下面有一个 TextView 吗?因为你在这里发布的 xml 中没有提到文本。 - Syed Ahmed Jamil
@SyedAhmedJamil 我在制作截图之前添加了TextView以便更好地理解,但忘记更新XML。已添加问题链接。 - pavelkorolevxyz
@P_King 你尝试过使用ConstraintLayout而不是RelativeLayout吗?因为我刚刚复制粘贴了你的代码,只是排除了相对布局标签,因为默认情况下我使用约束布局,并且它完美地运行。我可以向下滚动并在中间按钮上方看到完整的文本。我将在一会儿发布适当的答案,附带有GIF。 - Syed Ahmed Jamil
@SyedAhmedJamil 录制了视频。在这个视频中将 RL 更改为 CL。https://imgur.com/a/w1fYQEI - pavelkorolevxyz
显示剩余13条评论
4个回答

5

如果您的活动需要软输入调整类型,您只需在清单文件中定义即可。您可以在清单文件中添加以下行。

 <activity android:name=".Main2Activity" android:theme="@style/BaseTheme_FullScreen2"
              android:windowSoftInputMode="adjustResize|adjustPan">
    </activity>

不,这并没有帮助。adjustPan 会使按钮留在键盘后面,内容也无法在键盘上方滚动。adjustResize 的情况也没有改变 - https://imgur.com/GwOtLIq - pavelkorolevxyz

0
我对安卓不是很了解,但作为Java程序员,我建议您在Activity类中更改scrollView的大小。 请将您的scrollView放入其他CordinatorLayout中,并在init()或onCreate()方法中添加以下代码。

 protected void onCreate(Bundle savedInstanceState) {
   
    mySwipeRefreshLayout.setOnRefreshListener(
            new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                  scrollView.setMinHeight(layoutComposite.computeSize(layoutComposite.getSize().x+100 , SWT.DEFAULT).y+100);
    
                }
            }
    );
}

在活动的refresh/onload/onCreate方法中调用上面的代码

Intent intent=new Intent(Current_Activity.this,Current_Activity.class);
    startActivity(intent);
    finish();

请参考此链接滚动条实现


0
如果您想在键盘打开时实现滚动,必须调整小部件的顺序,以便它们不会相互覆盖。
scrollview小部件中添加android:layout_above="@+id/button",这样当键盘显示时,它就会自动调整到按钮上方。 代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="80dp"
        android:layout_above="@+id/button"
        android:clipToPadding="false">

        <LinearLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="16dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher" />

            ...


            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Eighth" />

        </LinearLayout>

    </ScrollView>

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_margin="16dp"
        android:text="Button" />

</RelativeLayout>

enter image description here


这就否定了我做这件事的初衷。我想要看到“Button”背后的内容,同时也希望在键盘弹出时EditText可见。 - pavelkorolevxyz

0

在编程中,使用CoordinatorLayout代替RelativeLayout,如下所示:

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

<ScrollView
        android:id="@+id/scroll_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <LinearLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="16dp">

        <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher" />

        ...


        <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Eighth" />

    </LinearLayout>

</ScrollView>

<Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        app:layout_anchorGravity=”bottom|end”
        android:layout_margin="16dp"
        app:layout_anchor=”@id/scroll_container” 
        app:layout_dodgeInsetEdges="bottom"
        android:text="Button" />

</CoordinatorLayout>

我没有测试过,但是在清单文件中为activity标记设置android:windowSoftInputMode="adjustResize|adjustPan",应该能够按照你的意愿工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接