how to make an android view scrollable when the ke

2020-02-08 08:14发布

问题:

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.

回答1:

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:

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


回答2:

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

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

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

<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>


回答3:

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>



回答4:

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

<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>