简体   繁体   中英

Android Fragment view does not scroll when keyboard is up

I am loading a fragment like this

String tag = fragment.getClass().getSimpleName();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(android.R.id.content, fragment,tag);
transaction.addToBackStack(tag);
transaction.commit();

The activity has this property set

android:windowSoftInputMode="adjustResize"

In my view I have a button at the bottom that I want to keep on top of the keyboard. This is what my fragment layout file looks like

<RelativeLayout 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:layout_alignParentTop="true"
    android:fillViewport="true"
    android:isScrollContainer="true">

         <LinearLayout..../>

    </ScrollView>

    <Button
    android:id="@+id/activate_button"
  style="@android:style/Widget.DeviceDefault.Light.Button.Borderless.Small"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" />

 </RelativeLayout>

I have an EiditText in this view and when I request focus the soft keyboard appears but view doesn't scroll.

Can anyone tell me why view is not resizing when keyboard is up?

I found two solutions that worked for me

Put this in the onCreate of your fragment:

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

Or

Use a ScrollView as parent of your FrameLayout in the Activity, like this:

<ScrollView>
    <FrameLayout/>
</ScrollView>

Try this, Change Parent RelativeLayout `

android:layout_height="wrap_content" into android:layout_height="match_parent"

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


    <Button
        android:id="@+id/activate_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="10dp"/>

</RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM