简体   繁体   English

键盘弹起时,Android Fragment视图不滚动

[英]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. 我在此视图中有一个EiditText,当我请求聚焦时,软键盘出现,但视图不滚动。

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: 将其放在片段的onCreate中:

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作为Activity中FrameLayout的父级,如下所示:

<ScrollView>
    <FrameLayout/>
</ScrollView>

Try this, Change Parent RelativeLayout ` 尝试此操作,更改Parent RelativeLayout`

android:layout_height="wrap_content" into android:layout_height="match_parent" android:layout_height =“ wrap_content”转换为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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Fragment中的ScrollView在软键盘出现时不滚动 - ScrollView in Fragment does not scroll when the soft keyboard shows up 当软键盘在片段中启动且工具栏和 windowTranslucentStatus 设置为 true 时滚动视图 - Scroll view when soft keyboard is up in fragment with Toolbar and windowTranslucentStatus set to true Expo Android:为什么出现键盘时视图会向上移动? - Expo Android: Why does the view move up when the keyboard appears? 键盘识别滚动视图在Android上不起作用 - keyboard aware scroll view does not work on android 键入片段时键盘隐藏编辑文本的位置不向上滚动 - keyboard hide edit text position not scroll up when typing in fragment 防止Android软键盘在片段中向上移动视图? - Preventing the Android soft keyboard from moving the view up in a Fragment? 如何在键盘出现时在列表视图中向上滚动 - How to make scroll up in list view when keyboard is appear 当键盘可见时,Android Dialogue Fragment 不会向上移动 - Android Dialogue Fragment not moving up when keyboard is visible android-ScrollView不会在键盘向上滚动 - android - ScrollView doesn't scroll when keyboard is up 如何在Android键盘弹出时滚动布局 - How to scroll layout when keyboard pops up in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM