首页 > 解决方案 > 当单击文本输入之一以调出键盘时,使背景图像不会“挤压”

问题描述

所以我是编码 android 的初学者,通常我可以在谷歌上找到我的解决方案,但这次我没有运气。我制作了一个包含两个活动的应用程序(一个登录屏幕,也有一个注册按钮,该按钮通向一个注册屏幕),在初始登录屏幕上,单击其中一个文本输入挤压背景图像,基本上适合它当键盘占用一些屏幕时产生的较小空间。这很烦人。

很抱歉粘贴了这么大的代码块,但作为初学者,我不确定这段代码的哪一部分是相关的,所以我已经包含了整个 xml。谢谢!

哦,再一次 - 我不能强调我是一个完全的菜鸟 - 所以任何答案都可以明确说明我应该更改/包含哪些代码以及在哪个文件中,以及该文件中的位置?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:background="@drawable/qlogoonwallpaper"
    tools:context=".MainActivity">

    <!-- Login progress -->
    <ProgressBar
        android:id="@+id/login_progress"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:visibility="gone" />

    <ScrollView
        android:id="@+id/login_form"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="32dp"
        android:layout_marginBottom="48dp">

        <LinearLayout
            android:id="@+id/email_login_form"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <AutoCompleteTextView
                    android:id="@+id/email"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:dropDownWidth="match_parent"
                    android:hint="@string/prompt_email"
                    android:inputType="textEmailAddress"
                    android:maxLines="1"
                    android:singleLine="true" />

            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:id="@+id/password"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/prompt_password"
                    android:imeActionId="6"
                    android:imeActionLabel="@string/action_sign_in_short"
                    android:imeOptions="actionUnspecified"
                    android:inputType="textPassword"
                    android:maxLines="1"
                    android:singleLine="true" />

            </android.support.design.widget.TextInputLayout>

            <Button
                android:id="@+id/email_sign_in_button"
                style="?android:textAppearanceSmall"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="16dp"
                android:background="@color/colorPrimaryDark"
                android:text="@string/action_sign_in"
                android:textAllCaps="false"
                android:textColor="@android:color/background_light"
                android:textStyle="bold" />
        </LinearLayout>
    </ScrollView>

    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="-208dp"
        android:ems="10"
        android:hint="@string/prompt_email"
        android:inputType="textEmailAddress" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="225dp"
        android:background="@android:color/transparent"
        android:text="@string/register_intro"
        android:textAllCaps="false"
        android:textColor="@android:color/background_light"
        android:textSize="18sp"
        android:textStyle="bold|italic" />

</RelativeLayout>

标签: javaandroidbackground-image

解决方案


在 Android Manifest 文件中,尝试为您的 MainActivity 添加以下属性:

<activity
            android:name=".MainActivity"
            android:windowSoftInputMode="adjustPan">
...
</activity>

当软键盘出现但活动的窗口未调整大小时,这会使视图平移。看看这是否有什么不同。如果调整大小会更好,而不是"adjustPan"属性,您可以添加"adjustResize". 希望两者中的一个能为您解决问题。


推荐阅读