java - 当键盘出现时屏幕没有向上滚动

标签 java android xml scroll keyboard

键盘出现时屏幕不会向上滚动。我读过很多答案说要使用 android:windowSoftInputMode="something" 我尝试了其中很多

android:windowSoftInputMode="adjustPan|adjustResize" 

android:windowSoftInputMode="adjustResize" 

android:windowSoftInputMode="adjustPan" 

android:windowSoftInputMode="stateVisible|adjustResize"

android:windowSoftInputMode="stateVisible"

(也尝试了其他一些)它们都不起作用

我认为问题出在我的 xml 上,其中大部分是由 android studio“Activity 登录”模板生成的。

还有其他办法吗?

我的 xml 文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.eduardo.academia.LoginActivity">

    <!-- 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" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="211dp"
        android:layout_height="235dp"
        app:srcCompat="@drawable/logo"
        android:windowSoftInputMode="stateHidden"/>

    <LinearLayout

        android:id="@+id/login_form"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <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: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="@+id/login"
                    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="wrap_content"
                android:layout_marginTop="16dp"
                android:text="@string/action_sign_in"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/txtMudarSenha"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="40dp"
                android:gravity="center"
                android:text="@string/forgot_password"
                android:onClick="mudarSenha"
                android:clickable="true"/>

        </LinearLayout>
    </LinearLayout>
</LinearLayout>

Android list

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.eduardo.academia">

    <!-- To auto-complete the email text field in the login form with the user's emails -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_PROFILE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:windowSoftInputMode="stateVisible|adjustResize"
            android:name=".LoginActivity"
            android:label="@string/title_activity_login"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ExerciciosActivity"
            android:label="@string/title_activity_exercicios"
            android:theme="@style/AppTheme.NoActionBar"></activity>
    </application>

</manifest>

最佳答案

您应该为此使用 ScrollView。尝试使用下面的 xml 进行布局

 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.eduardo.academia.LoginActivity">

    <!-- 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" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="211dp"
        android:layout_height="235dp"
        app:srcCompat="@drawable/logo"
        android:windowSoftInputMode="stateHidden"/>

    <LinearLayout

        android:id="@+id/login_form"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <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: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="@+id/login"
                    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="wrap_content"
                android:layout_marginTop="16dp"
                android:text="@string/action_sign_in"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/txtMudarSenha"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="40dp"
                android:gravity="center"
                android:text="@string/forgot_password"
                android:onClick="mudarSenha"
                android:clickable="true"/>

        </LinearLayout>
    </LinearLayout>
</LinearLayout>
</ScrollView>

关于java - 当键盘出现时屏幕没有向上滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46514928/

相关文章:

java - 如何在每次请求后将对象添加到模型中(Spring Boot)

java - 为什么代码不更改 .properties 文件

android - Firebase 身份验证 - 未处理的异常 : [firebase_auth/admin-restricted-operation] This operation is restricted to administrators only

java - 在 Android 中读取和显示大格式文本文档

python - 元素树搜索帮助(python, xml)

java - 如何使用 OAuth 1.0 在 java 中创建 Rest Web 服务

android - Quasar 中不显示网站图标和启动屏幕

java - "center_horizontal|center_vertical"和 "center"有区别吗

java - Struts 从 2.2.1 升级到 2.5.30 后的 404 操作

java - 如何检查用户是否成功购买