Android:如何防止键盘在我的布局中向上推某个视图? [复制]

Posted

技术标签:

【中文标题】Android:如何防止键盘在我的布局中向上推某个视图? [复制]【英文标题】:Android: How to prevent the keyboard from pushing a certain view up in my layout? [duplicate] 【发布时间】:2021-03-22 01:32:19 【问题描述】:

在我的布局中,我有一个编辑文本和一个图像视图。图像视图高度设置为匹配父布局。

我想要发生的是,当用户选择编辑文本并出现键盘时,编辑文本应该在键盘上方,而图像视图应该保持在键盘出现之前的确切位置。现在整个布局被向上推,但我希望图像视图不要移动。

我尝试过使用 adjustPan、adjustResize、adjustNothing,但它们并没有达到我想要的效果。

在键盘之前。我需要这个图像在键盘出现后准确地出现在它的位置。

但图像向上移动,这不是我想要的。基本上我需要键盘来重叠图像。

这是我的 xml 布局:

<androidx.constraintlayout.widget.ConstraintLayout 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:layout_
android:layout_
android:background="@android:color/black">

<ImageView
    android:layout_
    android:layout_
    android:id="@+id/image_view"
    android:scaleType="fitCenter"/>

<View
    android:layout_
    android:layout_
    android:id="@+id/transparent_view"
    android:background="@android:color/black"
    android:alpha="0.6"
    app:layout_constraintTop_toTopOf="@id/message_edit_text"
    app:layout_constraintBottom_toBottomOf="parent"/>

<ImageView
    android:layout_
    android:layout_
    android:id="@+id/gallery_icon"
    app:layout_constraintDimensionRatio="1:1"
    tools:src="@drawable/gallery_icon"
    android:padding="10dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintBottom_toBottomOf="@id/message_edit_text"
    app:layout_constraintTop_toTopOf="@id/message_edit_text"/>

<EditText
    android:layout_
    android:layout_
    android:id="@+id/message_edit_text"
    android:hint="Type a message..."
    android:layout_marginBottom="10dp"
    android:padding="10dp"
    android:textColor="@color/white"
    android:maxLines="5"
    android:textColorHint="@android:color/darker_gray"
    android:background="@null"
    app:layout_constraintLeft_toRightOf="@+id/gallery_icon"
    app:layout_constraintRight_toLeftOf="@id/send_button"
    app:layout_constraintBottom_toBottomOf="parent"/>

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:layout_
    android:layout_
    android:id="@+id/send_button"
    android:layout_marginBottom="12dp"
    android:layout_marginRight="12dp"
    android:clickable="true"
    android:visibility="visible"
    android:backgroundTint="@color/green"
    app:borderWidth="0dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="@id/transparent_view"
    app:layout_constraintBottom_toTopOf="@id/transparent_view"/>

</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

这将有助于分享您的 xml 布局的一些代码,以便我们了解为什么这些清单设置可能无法正常工作 我认为这在接受的答案中特别重复,它说:“如果您的容器没有改变大小,那么您可能将高度设置为“匹配父级”。如果可能,设置父级到“包装内容”,或约束布局到父级的顶部和底部。” 不,我尝试包装内容并使用约束布局,并限制顶部和底部。不工作 那么请发布一些代码,以便我们可以复制您的问题,除非您只是想在黑暗中回答问题 该问题还建议使用可滚动视图。我不希望它滚动 【参考方案1】:

您可能想检查您的 AndroidManifest.xml 并搜索您的活动。如果你找到了android:windowSoftInputMode="adjustResize,就删除它。

【讨论】:

以上是关于Android:如何防止键盘在我的布局中向上推某个视图? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

如何防止在android中打开键盘时反应本机模式向上移动?

如何防止键盘在 SwiftUI 中向上推视图? [复制]

Android:如何按下软键盘上方的按钮

使用 phonegap 防止键盘在 iOS 应用程序中向上推 webview

Android Studio:如何防止工具栏中的向上按钮将子视图向右推?

当键盘可见时,离子页脚不会被向上推(ios 和 android)