android - adjustResize 不适用于 fragment 布局内的 EditText View

标签 android android-fragments android-activity android-edittext window-soft-input-mode

我有一个全屏 Activity ,其中包含某些布局元素,这些元素隐藏起来让位于容器 (FrameLayout) 内的 fragment 。布局如下图:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/home_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/closing_background"
    android:orientation="vertical"
    tools:context="com.example.activities.MainActivity">

        <FrameLayout
            android:id="@+id/dim"
            android:alpha="0"
            android:fitsSystemWindows="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/dimBg" />

        <RelativeLayout
            android:id="@+id/header_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="48dp"
            android:layout_alignParentTop="true">

            <ImageView
                android:id="@+id/menu"
                android:visibility="visible"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_marginStart="24dp"
                android:background="@drawable/ic_menu_black_24dp"/>

            <ImageView
                android:id="@+id/logo_img"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:scaleType="fitXY"
                android:src="@drawable/ic_app_logo_black" />

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/header_layout"
            android:orientation="vertical"
            android:layout_marginTop="26dp">

            <FrameLayout
                android:id="@+id/fragmentContainer"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

        </RelativeLayout>

        <LinearLayout
           android:id="@+id/first_layout"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_below="@id/header_layout"
           android:visibility="gone"
           android:orientation="vertical"
           android:layout_marginTop="93dp">

             <--other layout elements here-->
        </LinearLayout>
</RelativeLayout>

fragment 布局如下图:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/parentLayout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:background="@android:color/transparent">

        <RelativeLayout
            android:id="@+id/rootView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipChildren="false">

            <TextView
                android:id="@+id/description"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="6dp"
                android:layout_alignParentTop="true"
                android:textSize="18sp"
                android:lineSpacingMultiplier="1.5"/>

            <com.example.MyCustomRecyclerView
                android:id="@+id/customRecyclerView"
                android:layout_below="@id/description"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="16dp"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginBottom="72dp"
                android:clipToPadding="false">

            </com.example.MyCustomRecyclerView>

            <include layout="@layout/input_view"
                tools:visibility = "gone"/>

        </RelativeLayout>

</ScrollView>

如您所见,在我的 fragment 布局中,我包含了另一个名为 input_view 的布局。其布局文件如下所示:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_slide_up_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.activities.MainActivity"
    tools:showIn="@layout/activity_main">

    <RelativeLayout
        android:id="@+id/slideResponseView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="26dp"
        android:background="@drawable/rounded_up_corners_bg">

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/pullDownArrow"
            android:text="@string/response_view_title"
            android:gravity="center_horizontal"
            android:layout_marginTop="16dp"
            android:textAllCaps="false"/>

        **<EditText
            android:id="@+id/response_view_input"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/title"
            android:layout_margin="10dp"
            android:gravity="top"
            android:padding="10dp"/>**

        <ImageView

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            app:srcCompat="@drawable/ic_keyboard_arrow_down_black_24dp"
            android:id="@+id/pullDownArrow"
            android:layout_marginTop="15dp"/>

    </RelativeLayout>

</RelativeLayout>

这是从以下来源获得的 slideUp 面板:https://github.com/mancj/SlideUp-Android ,这主要是一个 edittext View ,它在 recyclerview 项目上的点击事件上向上滑动。 slider 面板在纵向模式下占据了 2/3 的屏幕空间(我的应用程序只有纵向模式)。现在,当我单击此 slider 面板上的 EditText View 以输入任何文本时,我希望此 View 在软键盘从其下方打开时调整大小。我尝试了一些关于 SO 的建议,比如用 ScrollView 包装根布局,但仍然不起作用。

此外, list 文件中的 Activity 包括以下内容:

android:windowSoftInputMode="adjustResize"

如上所述,该 Activity 是一个全屏 Activity ,我使用以下代码以编程方式处理它:

requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

View decorView = getWindow().getDecorView();

int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
decorView.setSystemUiVisibility(uiOptions);

有人可以在这里指导我吗?如果问题或我的部分代码不清楚,请告诉我,以便我详细说明。

谢谢,

AB

最佳答案

将 ScrollView 替换为 NestedScrollView。 同时将 RelativeLayout 替换为 ConstraintLayout。 其他代码没问题。

关于android - adjustResize 不适用于 fragment 布局内的 EditText View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58642135/

相关文章:

java - 无法解析字符串日期

android - 具有透明状态栏的全屏 fragment (以编程方式)

java - Android 为什么文本显示在onCreate

android - 将消息从 Activity 发送到服务 - Android

安卓 : Presenting transparent activity causes crash

android - V/FA : Inactivity, 断开服务问题

java - 如何使音池静音?

java - 在 Android 中未收到位置更新

android - 在 Fragment 中设置焦点

android - 如何在 ButterKnife 中绑定(bind) XML fragment ?