我有一个带有 ViewPager2 的 TabLayout,我在我的主要活动布局中设置了这些。在不同的布局中,我有与片段相关的布局,即主要活动托管的布局。
问题是,在运行时,当我单击 EditText 或 RecyclerView 中有要填充的项目时,即使没有单击 EditText,它也会全屏显示。
RecyclerView 为空且我不点击 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>