出现键盘时如何使android视图可滚动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10599451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:17:43  来源:igfitidea点击:

how to make an android view scrollable when the keyboard appears?

androidandroid-layoutandroid-scrollviewandroid-keypad

提问by Alexis

I have a view with some fields (name, email, password, register button) :

我有一些字段(姓名、电子邮件、密码、注册按钮)的视图:

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ViewFlipper
        android:id="@+id/viewflipper"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        //Layouts

    </ViewFlipper>

</ScrollView>

When I focus an edittext field, the keyboard appears and is overlaying the lower fields on the view. So I can't see them when the keyboard is present. I would like to know how to make the view scrollable so that I can see the lower fields. And additionally, how to make the view scroll automatically to make the focused field visible.

当我聚焦编辑文本字段时,键盘出现并覆盖视图上的较低字段。所以当键盘存在时我看不到它们。我想知道如何使视图可滚动,以便我可以看到较低的字段。此外,如何使视图自动滚动以使聚焦的字段可见。

回答by Aleks G

You need to include android:windowSoftInputMode="adjustResize"attribute for your activity in the AndroidManifest.xml file - and then Android should do the resizing for you automatically:

您需要android:windowSoftInputMode="adjustResize"在 AndroidManifest.xml 文件中为您的活动包含属性 - 然后 Android 应该自动为您调整大小:

<activity android:name="..."
          ...
          android:windowSoftInputMode="adjustResize">
    ...
/>

回答by swiftBoy

I have created simple xml file, hope this is what you are looking for.

我已经创建了简单的 xml 文件,希望这是您正在寻找的。

step 1. you can scroll when key-pad is appeared.

第 1 步。您可以在出现键盘时滚动。

step 2. when you click on Text-Field it will come on Focus/above keypad.

第 2 步。当您单击文本字段时,它将出现在焦点/上方的键盘上。

<ScrollView android:id="@+id/ScrollView01"
xmlns:android="http://schemas.android.com/apk/res/android"
android:fillViewport="true" 
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout 
    android:orientation="vertical"
    android:layout_width="fill_parent" 
    android:weightSum="1.0" 
    android:layout_height="match_parent"
    android:focusable="true"
    android:focusableInTouchMode="true" 
    android:id="@+id/l_layout">
    <EditText 
        android:layout_height="wrap_content" 
        android:layout_width="match_parent" 
        android:id="@+id/editText1" 
        android:layout_marginTop="100dp">
        <requestFocus></requestFocus>
    </EditText>
    <EditText 
        android:layout_height="wrap_content" 
        android:layout_width="match_parent" 
        android:id="@+id/editText2" 
        android:layout_marginTop="100dp">
        </EditText>
    </LinearLayout> 
</ScrollView>

回答by Sandeep Sharma

In my case @Aleks G solution was not working. so I resolved my problem with

就我而言,@Aleks G 解决方案不起作用。所以我解决了我的问题

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/registration_scroll_view"
    android:layout_alignParentTop="true"
    android:layout_above="@+id/btn_register_submit"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical">

       <!--This comment will be replaced 
           by your focusable views-->

    </LinearLayout>
</ScrollView>

<Button
    android:id="@+id/btn_register_submit"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:background="@drawable/form_action_button_green"
    android:gravity="center"
    android:text="@string/submit"
    />

</RelativeLayout>

回答by Rafael T

Have you tried this:

你有没有试过这个:

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ViewFlipper
    android:id="@+id/viewflipper"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
    <ScrollView 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/scroll_container">
        //Layouts
    </ScrollView>


</ViewFlipper>