Android:如何防止软键盘将我的视图向上推?
我的应用程序底部有一个垂直滑动抽屉。当软键盘打开时,它会将抽屉的标签向上推,使其位于键盘顶部。我实际上希望它保留在屏幕底部,在显示键盘时隐藏。
还有其他人遇到这个问题吗?知道如何解决吗?
I have a vertical sliding drawer at the bottom of my app. When the soft keyboard opens, it pushes the tab for the drawer up, so it sits atop the keyboard. I actually want it to remain at the bottom of the screen, becoming hidden when the keyboard is shown.
Anyone else run into this issue? Know how to fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
您只需在 Activity 标记内的
AndroidMainfest.xml
文件中将 Activity 的windowSoftInputMode
标志切换为adjustPan
即可。有关详细信息,请查看官方文档。
如果您的容器没有改变大小,那么您可能将高度设置为“匹配父级”。如果可能,将父级设置为“Wrap Content”,或将约束布局设置为父级的顶部和底部。
父容器将缩小以适应可用空间,因此您的内容可能应该位于滚动视图内,以防止(取决于手机制造商和选择的布局...)
即使其所在的布局是相对或约束布局,该内容也可能会出现问题 1-3。
You can simply switch your Activity's
windowSoftInputMode
flag toadjustPan
in yourAndroidMainfest.xml
file inside your activity tag.Check the official documentation for more info.
If your container is not changing size, then you likely have the height set to "match parent". If possible, set the parent to "Wrap Content", or a constraint layout with constraingts to top and bottom of parent.
The parent container will shrink to fit the available space, so it is likely that your content should be inside of a scolling view to prevent (depending on the phone manufacturer and the layout choosen...)
even if the layout it is in is a relative or constraint layout, the content could exhibit problems 1-3.
所有答案都不适合我,但这确实有效,将此属性添加到
AndroidManifest.xml
中的activity
标记中:None of the answers worked for me, but this did the trick, add this attribute to the
activity
tag in yourAndroidManifest.xml
:就我而言,按钮被推起的原因是因为它们上方的视图是一个 ScrollView,并且无论 android 的值是什么,它都会随着键盘上方推起的按钮而折叠: windowSoftInputMode 我正在设置。
通过在按钮上方的
ScrollView
上设置android:isScrollContainer="false"
,我能够避免底行按钮被软键盘推起。In my case, the reason the buttons got pushed up was because the view above them was a
ScrollView
, and it got collapsed with the buttons pushed up above the keyboard no matter what value ofandroid:windowSoftInputMode
I was setting.I was able to avoid my bottom row of buttons getting pushed up by the soft keyboard by setting
android:isScrollContainer="false"
on theScrollView
that sits above the buttons.您可以尝试动态添加此属性,方法是将以下代码放入活动的
onCreate
方法中:这对我有用,但那:
没有。
You can try to add this attribute dynamically, by putting the following code in the
onCreate
method of your activity:This worked for me, but that:
didnt.
这里的这些答案对我没有帮助。所以我尝试了这个:
这就像一个魅力,现在我的应用程序的标题不会消失。其更平滑。
These answers here didn't help me. So I tried this:
This worked like a charm, Now the header of my app doesn't disappear. Its smoother.
要在片段中以编程方式执行此操作,您可以使用以下代码
将其放在 onResume() 中
To do this programatically in a fragment you can use following code
Place this in onResume()
这对我有用
This one worked for me
只需添加一行...
在
manifest 文件
的所需活动中添加android:windowSoftInputMode="stateHidden|adjustPan"
。我刚刚解决了:):)
Just a single line to be added...
Add
android:windowSoftInputMode="stateHidden|adjustPan"
in required activity of yourmanifest file
.I just got solved :) :)
对于未来的读者。
我想要对此问题进行具体控制,所以这就是我所做的:
从片段或活动中,隐藏您的其他视图(键盘打开时不需要这些视图) ),然后恢复它们来解决这个问题:
For future readers.
I wanted specific control over this issue, so this is what I did:
From a fragment or activity, hide your other views (that aren't needed while the keyboard is up), then restore them to solve this problem:
到目前为止,答案对我没有帮助,因为我在 textView 下面有一个按钮和一个 textInput 字段(并排),它一直被键盘隐藏,但这已经解决了我的问题:
So far the answers didn't help me as I have a button and a textInput field (side by side) below the textView which kept getting hidden by the keyboard, but this has solved my issue:
对于xamarin用户,将此代码添加到MainActivity类的Activity属性中,
或者您可以将此代码
Window.SetSoftInputMode(Android.Views.SoftInput.AdjustNothing)
添加到MainActivity类的OnCreate方法中。For xamarin users add this code to Activity attribute of the MainActivity class,
or you can add this code
Window.SetSoftInputMode(Android.Views.SoftInput.AdjustNothing)
to the OnCreate method of MainActivity class.这是最适合我的,
试试吧!
This was the best which worked for me
Try it!
将以下代码添加到清单文件的“活动”中。
Add following code to the 'activity' of Manifest file.
这段代码有效。
This code works.
好吧,我已经看过这些答案,但就我而言,我遇到了同样的问题,并通过一个非常方便且最简单的解决方案获得了庇护,该解决方案涉及在 xml 文件中的 Scrollview 标记中放置一个非常小的无辜属性。那就是
祝你好运!
Well i have watched these answers but in my case i fell into the same issue and got refuge through a very handy and easiest solution that involves putting a very small innocent attribute in your Scrollview tag residing in your xml file. That is
Good luck!
这个对我有用。
This one is working for me.
我通过在清单文件中添加添加解决了我的问题
。
并将 Recyclerviews 约束 isScrollContainer 设置为 false 。
I have solved my issue by adding
In manifest file add.
and making the Recyclerviews constraint isScrollContainer to false .
有两种方法可以解决这个问题。
转到 AndroidManifist.xml,并在您的活动名称中添加此行
如下面的代码所示,我已添加到注册活动。
在第二种方式中,转到您的活动,然后在 onCreate 方法中添加此代码。
There are two ways of solving this problem.
Go to the AndroidManifist.xml, and in the name of your activity, add this line
As in the below code, I have added to the Register Activity.
In the second way, go to your activity, and in your onCreate method, add this code.
尝试使用这个:
Try to use this:
对于滚动视图:
如果在 Android Manifest 中添加
android:windowSoftInputMode="stateHidden|adjustPan"
后仍然不起作用。它可能会受到影响,因为当键盘出现时,它将进入滚动视图,如果您的按钮/任何对象不在滚动视图中,那么这些对象将跟随键盘并移动其位置。
检查按钮所在的 xml,并确保它位于滚动视图括号下方而不是超出滚动视图括号之外。
希望这会有所帮助。 :D
For Scroll View:
if after adding
android:windowSoftInputMode="stateHidden|adjustPan"
in your Android Manifest and still does not work.It may be affected because when the keyboard appears, it will be into a scroll view and if your button/any objects is not in your scroll view then the objects will follow the keyboard and move its position.
Check out your xml where your button is and make sure it is under your scroll View bracket and not out of it.
Hope this helps out. :D
就我而言,我需要键盘保持隐藏状态,并且在单击按钮后我的布局需要调整,所以我只是在清单中添加了这个命令,它变得非常正确。
In my case I needed the keyboard to stay hidden and just after the click of the button my layout needs to be adjusted, so I just added this command in the manifest and it got super right.
当您想在打开键盘时隐藏视图时。
将其添加到清单文件中的活动中
When you want to hide view when open keyboard.
Add this into your Activity in manifest file
他们都不适合我,试试这个
None of them worked for me, try this one
我为这个问题苦苦挣扎了一段时间。一些解决方案有效,但是我的一些观点仍然被推崇,而另一些则没有......所以它并没有完全解决我的问题。最后,这项工作的作用是将以下代码行添加到我的活动标记中的清单中......
祝你好运
I was struggling for a while with this problem. Some of the solutions worked however some of my views where still being pushed up while others weren't... So it didn't completely solve my problem. In the end, what did the job was adding the following line of code to my manifest in the activity tag...
Good luck
该活动的主窗口不会调整大小以为软键盘腾出空间。相反,窗口的内容将自动平移,以便当前焦点永远不会被键盘遮挡,并且用户始终可以看到他们正在键入的内容。
这可能是您想要的更好的解决方案。
The activity's main window will not resize to make room for the soft keyboard. Rather, the contents of the window will be automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing.
This might be a better solution for what you desired.
这段代码可能对你有帮助。在您的
oncreate
方法中使用它。This code may help you. Use it in your
oncreate
method.包含在您要显示的活动下的清单文件中。但请确保不使用全屏活动
Include in your manifest file under activity which you want to display .But make sure not using Full screen Activity
@manifest 在您的活动中:
@manifest in your activity:
就我而言 - 软键盘向上移动背景图像 - 这里发布的答案都不起作用。不过我找到了解决方案。
设置整个屏幕(活动)的背景图像:
最后,背景图像将保持固定在适当的位置
In my case - soft keyboard moving up the BACKGROUND image - none of the answers published here worked. However I found a solution.
Set the background image for the whole screen (activity):
In the end, the background image will remain fixed in place
我使用的是透明状态栏,所以对我来说没有任何作用。隐藏状态栏不是透明的状态栏,而是对我有用
,并将其添加到清单中的活动
对我有用
I was using transparent status bar so nothing worked for me. Instead of transparent statusbar hiding the status bar worked for me
and added this for activity in manifest
Worked for me