在片段布局中,EditText视图使用adjustResize无法正常工作

3

我有一个全屏的活动,其中某些布局元素会隐藏以给容器(FrameLayout)内的片段让路。布局如下所示:

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

碎片布局如下所示:
<?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>

正如您所看到的,在我的片段布局中,我包含了另一个名为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>

这是从https://github.com/mancj/SlideUp-Android获取的一个slideUp面板,它主要是一个EditText视图,会在RecyclerView项目的点击事件上滑动。在纵向模式下,滑块面板占据了屏幕空间的2/3(我的应用仅限于纵向模式)。现在,当我点击这个滑块面板上的EditText视图以输入任何文本时,我希望当软键盘从下方打开时,此视图能够自动调整大小。我尝试了一些在SO上建议的方法,如将根布局包装在ScrollView中,但仍然无法解决问题。
此外,在清单文件中的活动包括以下内容:
android:windowSoftInputMode="adjustResize"

如上所述,这个活动是一个全屏活动,我可以使用下面的代码来编程处理:

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

1个回答

1
请使用NestedScrollView替换ScrollView。同时,请使用ConstraintLayout替换RelativeLayout。其他代码没有问题。

这两个变更都是在片段布局中,对活动布局没有任何变更吗? - Abhishek Sharma
adjustResize在ConstraintLayout中运作良好。因此,您应该将两者都替换掉。我认为这样是可行的。 - Mushfique Monim
我的问题是,我需要做的唯一更改是在片段布局中,还是活动布局也有任何更改? - Abhishek Sharma

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接