1

我有一个带有 ViewPager2 的 TabLayout,我在我的主要活动布局中设置了这些。在不同的布局中,我有与片段相关的布局,即主要活动托管的布局。

问题是,在运行时,当我单击 EditText 或 RecyclerView 中有要填充的项目时,即使没有单击 EditText,它也会全屏显示。

RecyclerView 为空且我不点击 EditText 时的布局:

在点击 EditText 之前

点击EditText后:

点击EditText后:

在我将代码粘贴到这里之前。您会在代码中注意到 LinearLayout 的高度是 wrap_content,但这并不重要,因为对于任何高度我都会设置它,问题就会发生。还有,属性

app:layout_constrainedHeight

或者它的任何变化都不能解决问题。但是,如果根 ViewGroup 是 RelativeLayout,那么问题就解决了。

现在,代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root_viewgroup_fragment_chat"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/color_chat"
tools:context=".ChatFragment">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerview_chat"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"/>

<!-- A horizontal line between the chatbox and RecyclerView -->
<View
    android:layout_width="0dp"
    android:layout_height="2dp"
    android:background="#dfdfdf"
    android:layout_marginBottom="0dp"
    app:layout_constraintBottom_toTopOf="@id/message_interface_layout"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"/>

<LinearLayout
    android:id="@+id/message_interface_layout"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:minHeight="48dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    android:background="@android:color/white">

    <EditText
        android:id="@+id/message_edittext"
        android:maxLines="5"
        android:layout_gravity="center"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
        android:hint="@string/hint_edittext_chat"
        android:background="@android:color/transparent"
        />

    <Button
        android:id="@+id/send_message_button"
        android:text="@string/chat_send_message"
        android:textSize="14sp"
        android:background="?attr/selectableItemBackground"
        android:clickable="true"
        android:focusable="true"
        android:layout_width="64dp"
        android:layout_height="48dp"
        android:gravity="center"
        android:layout_gravity="bottom" />

</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
4

2 回答 2

0

试试这个,我想这可能对你有帮助

  <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root_viewgroup_fragment_chat"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#fff"
    >

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview_chat"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <!-- A horizontal line between the chatbox and RecyclerView -->
    <View
        android:id="@+id/view"
        android:layout_width="0dp"
        android:layout_height="2dp"
        android:background="#dfdfdf"
        app:layout_constraintBottom_toTopOf="@+id/message_edittext"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <EditText
        android:id="@+id/message_edittext"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginStart="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginBottom="10dp"
        android:minHeight="64dp"
        android:layout_weight="1"
        android:background="@android:color/transparent"
        android:hint="message "
        android:inputType="textMultiLine"
        android:maxLines="5"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/send_message_button"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/send_message_button" />

    <Button
        android:id="@+id/send_message_button"
        android:layout_width="64dp"
        android:layout_height="48dp"
        android:layout_gravity="bottom"
        android:layout_marginBottom="10dp"
        android:background="?attr/selectableItemBackground"
        android:clickable="true"
        android:focusable="true"
        android:gravity="center"
        android:text="send message"
        android:textSize="14sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
于 2020-07-28T01:42:14.287 回答
0

对于其他有屏幕调整问题的人。我在stackoverflow上搜索并没有找到任何解决方案。我到处都看到使用带有“adjustPan”或“adjustResize”或任何变体或这些的窗口软模式。

问题是我使用了没有 ActionBar 的应用程序主题,并且我实现了自己的 ToolBar。

解决方案是使用 AppCompat 或 MaterialComponents 提供的默认应用程序主题。

于 2020-07-28T13:09:41.987 回答