【问题标题】:AlignParentBottom button overlaps Textview on Scroll/FocusAlignParentBottom 按钮与 Scroll/Focus 上的 Textview 重叠
【发布时间】:2018-02-15 21:35:36
【问题描述】:

目前,我有一些带有多个 Textview 的 Android xml 布局,可以输入数据。相对布局的底部是一个属性为android:layout_alignParentBottom="true" 的按钮,带有文本“保存更改”,预计在键盘弹出时显示在键盘上方。

但是,当单击列表底部的文本视图时,一旦获得焦点,“保存更改”按钮就会与焦点所在的文本视图重叠。

注意:可以自己手动向下滚动一点,然后查看 Textview。

理想情况下,此处的功能是调整 scrollTo() 以有效地滚动到焦点元素并将其置于“保存更改”按钮上方的视图中。

我已尝试在清单的活动属性中设置此值:

android:windowSoftInputMode="adjustResize"

将相对布局包装在 LinearLayout 中,或在 ScrollView 底部添加填充均无济于事。

这是我当前的 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>

在键盘上方仍然显示“保存更改”按钮的同时滚动到焦点文本视图的最佳方式是什么?

非常感谢任何想法!

【问题讨论】:

    标签: android xml android-layout android-softkeyboard


    【解决方案1】:

    看起来你的布局大约是:

    <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 一直推到屏幕底部。

    【讨论】:

    • 感谢 Ben,这似乎大部分都在工作。但是,“保存更改”按钮上方似乎显示了一些空白/填充,该按钮仍与 Scrollview 重叠,并且它的来源不太明显,因为从按钮中删除填充并不能解决问题。 i.imgur.com/gCeolO4.png
    • @Alkarin 如果您将 &lt;Button&gt; 替换为 &lt;View&gt;(非常简单,没有边距/填充),它还会发生吗?
    • 认为我找到了罪魁祸首。从&lt;Scrollview&gt; 中删除android:paddingBottom="@dimen/activity_vertical_margin" 修复了它,我不知道为什么。你也可以更新你的答案,为什么将方向设置为垂直/将滚动视图高度设置为 0dp 有效?其他谢谢,非常感谢!我会接受你的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    • 2020-12-18
    • 1970-01-01
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多