假设我有这样一个简单的布局:
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=false
的ScrollView
中有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 JamilEditText
下面有一个TextView
吗?因为你在这里发布的 xml 中没有提到文本。 - Syed Ahmed JamilConstraintLayout
而不是RelativeLayout
吗?因为我刚刚复制粘贴了你的代码,只是排除了相对布局标签,因为默认情况下我使用约束布局,并且它完美地运行。我可以向下滚动并在中间按钮上方看到完整的文本。我将在一会儿发布适当的答案,附带有GIF。 - Syed Ahmed Jamil