目前,我有一些 Android xml 布局,其中包含多个可以输入数据的 Textview。相对布局的底部是一个按钮,其属性为 android:layout_alignParentBottom="true"
,文本为“Save Changes”,当键盘弹出时,该按钮应显示在键盘上方。
但是,当单击列表底部的 TextView 时,一旦聚焦,“保存更改”按钮就会与聚焦的 TextView 重叠。
注意:您可以自己手动向下滚动一点,然后查看 Textview。
理想情况下,这里的功能是调整 scrollTo() 以有效地滚动到焦点元素并将其放置在“保存更改”按钮上方的 View 中。
我已经尝试在 list 的 Activity 属性中设置这个值:
android:windowSoftInputMode="adjustResize"
在 LinearLayout 中包装 RelativeLayout,或在 ScrollView 的底部添加 padding 都无济于事。
这是我当前的 XML,不包括标题栏:
<ScrollView
android:id="@+id/edit_info_section"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:layout_below="@id/activity_title_border_bottom">
<LinearLayout
android:id="@+id/edit_info_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/huge_padding"
android:layout_marginBottom="@dimen/huge_padding">
<TextView
android:id="@+id/account_firstname_label"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/label_firstname"
style="@style/label_text"/>
<EditText
android:id="@+id/account_firstname_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:maxLines="1"
style="@style/EditTextStyle" />
<TextView
android:id="@+id/account_lastname_label"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/label_lastname"
style="@style/label_text"/>
<EditText
android:id="@+id/account_lastname_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:maxLines="1"
style="@style/EditTextStyle" />
<TextView
android:id="@+id/account_phone_label"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/label_phone"
style="@style/label_text"/>
<EditText
android:id="@+id/account_phone_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="phone"
android:maxLines="1"
style="@style/EditTextStyle" />
<TextView
android:id="@+id/account_email_label"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/label_email"
style="@style/label_text"/>
<EditText
android:id="@+id/account_email_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textEmailAddress"
android:maxLines="1"
android:layout_marginBottom="@dimen/activity_vertical_margin"
style="@style/EditTextStyle" />
<TextView
android:id="@+id/account_password_label"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/label_password"
style="@style/label_text"/>
<EditText
android:id="@+id/account_password_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
android:maxLines="1"
android:layout_marginBottom="@dimen/activity_vertical_margin"
style="@style/EditTextStyle" />
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/save_changes"
android:background="@drawable/button_background"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="@string/save_changes"
android:textColor="@color/White"
android:textStyle="bold"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin"
style="style/Widget.AppCompat.Button.Colored"/>
</RelativeLayout>
滚动到焦点 TextView 的最佳方式是什么,同时仍然在键盘上方显示“保存更改”按钮?
非常感谢任何想法!
最佳答案
看起来你的布局大概是这样的:
<RelativeLayout>
<ScrollView>
...
</ScrollView>
<Button/>
</RelativeLayout>
此结构的问题在于 ScrollView
会填满整个屏幕。换句话说,ScrollView
的一部分总是“位于”Button
之后……只有当键盘出现时才会出现问题。
相反,我认为您应该使用这种布局:
<LinearLayout
android:orientation="vertical"
...>
<ScrollView
android:layout_height="0dp"
android:layout_weight="1"
...>
...
</ScrollView>
<Button/>
</LinearLayout>
通过这样做,您可以确保 ScrollView
中的内容不可能位于按钮后面,但您仍然可以将 Button
一直推到屏幕底部。
关于android - AlignParentBottom 按钮与 Scroll/Focus 上的 Textview 重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48816682/