键盘启动时,Android Fragment视图不会滚动

问题描述 投票:0回答:2

我正在加载这样的片段

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();

活动已设置此属性

android:windowSoftInputMode="adjustResize"

在我看来,我在底部有一个按钮,我想要保持在键盘顶部。这就是我的片段布局文件的样子

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

我在此视图中有一个EditText,当我请求焦点时,软键盘出现,但视图不滚动。

任何人都可以告诉我为什么键盘启动时视图没有调整大小?

android android-fragments keyboard
2个回答
0
投票

试试这个,改变父亲RelativeLayout`

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>

0
投票

我找到了两个对我有用的解决方案

把它放在片段的onCreate中:

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

要么

在Activity中使用ScrollView作为FrameLayout的父级,如下所示:

<ScrollView>
    <FrameLayout/>
</ScrollView>
© www.soinside.com 2019 - 2024. All rights reserved.