Android 显示软键盘时向上移动布局?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1964789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Move layouts up when soft keyboard is shown?
提问by Dinedal
I have a few elements in a RelativeView with the align bottom attribute set, when the soft keyboard comes up the elements are hidden by the soft keyboard.
我在 RelativeView 中有几个元素,设置了 align bottom 属性,当软键盘出现时,这些元素被软键盘隐藏。
I would like them to move up so that if there is enough screen space they are shown above the keyboard, or to make the section above the keyboard scrollable so the user can still see the elements.
我希望它们向上移动,以便如果有足够的屏幕空间,它们会显示在键盘上方,或者使键盘上方的部分可滚动,以便用户仍然可以看到元素。
Any ideas on how to approach this?
关于如何解决这个问题的任何想法?
采纳答案by Christopher Orr
Yes, check out this article on the Android developers' sitewhich describes how the framework handles the soft keyboard appearing.
是的,请查看Android 开发人员站点上的这篇文章,该文章描述了框架如何处理出现的软键盘。
The android:windowSoftInputMode
attribute can be used to specify what happens on a per-activity basis: whether the layout is resized or whether it scrolls etc.
该android:windowSoftInputMode
属性可用于指定在每个活动的基础上发生的事情:布局是否调整大小或是否滚动等。
回答by Bobs
You can also use this code in onCreate()
method:
您还可以在onCreate()
方法中使用此代码:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
回答by Serhii Bohutskyi
Add in AndroidManifest.xml for your activity:
为您的活动添加 AndroidManifest.xml:
android:windowSoftInputMode="adjustPan|adjustResize"
回答by Jose Kurian
Make changes in the activity of your Manifest file like
在您的清单文件的活动中进行更改,例如
windowSoftInputMode="adjustResize"
OR
或者
Make changes in your onCreate()
method in the activity class like
onCreate()
在活动类中更改您的方法,例如
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
回答by Zahra.HY
In AndroidManifest.xml, don't forget to set:
在 AndroidManifest.xml 中,不要忘记设置:
android:windowSoftInputMode="adjustResize"
and for RelativeLayout inside ScrollView ,set :
对于 ScrollView 内的 RelativeLayout ,设置:
android:layout_gravity="center" or android:layout_gravity="bottom"
it will be okay
会好起来的
回答by Ronak Poriya
similar issue i was facing with my layout which consist scroll view inside. Whenever i try to select text in EditText,Copy/Cut/Paste action bar gets moved up with below code as such it does not resize layout
我的布局遇到了类似的问题,其中包含滚动视图。每当我尝试在 EditText 中选择文本时,复制/剪切/粘贴操作栏会随着下面的代码向上移动,因此它不会调整布局大小
android:windowSoftInputMode="adjustPan"
By modifying it to as below
通过将其修改为如下
1) AndroidManifest.xml
1) AndroidManifest.xml
android:windowSoftInputMode="stateVisible|adjustResize"
2) style.xml file ,in activity style
2) style.xml 文件,活动样式
<item name="android:windowActionBarOverlay">true</item>
It worked.!!!!!!!!!!!!
有效。!!!!!!!!!!!!
回答by Rahul_Pawar
for Activity in its oncreate methode insert below line
对于 Activity 在其 oncreate 方法中插入下面的行
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
and for fragments in its onCreate method
以及 onCreate 方法中的片段
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
回答by Heena Arora
There are 3 steps to follow to scroll screen up.
向上滚动屏幕有 3 个步骤。
- Write in manifest file for your activity:
- 为您的活动写入清单文件:
android:windowSoftInputMode="adjustResize"
android:windowSoftInputMode="adjustResize"
- Add following line in oncreate() method of your activity
- 在您的活动的 oncreate() 方法中添加以下行
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
- Then place your whole view in "Scrollview" in XML file like :
- 然后将整个视图放在 XML 文件中的“Scrollview”中,例如:
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:background="@drawable/bg" android:layout_height="match_parent" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_gravity="center" > <TextView /> ......... <TextView /> <EditText > <requestFocus /> </EditText> <Button /> </RelativeLayout> </ScrollView>
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:background="@drawable/bg" android:layout_height="match_parent" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_gravity="center" > <TextView /> ......... <TextView /> <EditText > <requestFocus /> </EditText> <Button /> </RelativeLayout> </ScrollView>
P.S. Don't forget to add android:layout_gravity="center" in parent Relative layout.
PS 不要忘记在父相对布局中添加 android:layout_gravity="center"。
回答by CoolMind
I tried a method of Diego Ramírez, it works. In AndroidManifest:
我尝试了 Diego Ramírez 的一种方法,它有效。在 AndroidManifest 中:
<activity
android:name=".MainActivity"
android:windowSoftInputMode="stateHidden|adjustResize">
...
</activity>
In activity_main.xml:
在activity_main.xml 中:
<LinearLayout ...
android:orientation="vertical">
<Space
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" />
<EditText
android:id="@+id/edName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/first_name"
android:inputType="textPersonName" />
<EditText ...>
...
</LinearLayout>
回答by Prakash Reddy
<activity name="ActivityName"
android:windowSoftInputMode="adjustResize">
...
</activity>
Add NestedScrollView around layout which you want to scroll, this will perfectly work
在要滚动的布局周围添加 NestedScrollView,这将完美地工作