Android输入法弹出,布局上移,背景不会压缩
来源:互联网 发布:jdbc mysql 编辑:程序博客网 时间:2024/06/07 04:03
转载请注明出处,谢谢:http://blog.csdn.net/harryweasley/article/details/50266749
Android的输入法弹出问题,一直是困扰很多开发人员的问题,当输入法弹出时,布局会被压缩,某些控件被遮挡住,但是需求可能并不想让该控件遮挡住,在做输入法的时候,你一定要知道这个属性,android:windowSoftInputMode,他有三个属性,分别是adjustUnspecified,adjustResize,adjustPan。我建议最好不要用默认的,即adjustUnspecified。关于更多这三个属性的知识,我之前的一个博客写的很详细,地址是:http://blog.csdn.net/harryweasley/article/details/49124385
如下图所示:
当输入法弹出来的时候,会遮盖住登录按钮,但是我现在并不想输入法遮盖住登录按钮,同时希望背景也不会被压缩。
现在的需求是,当输入法弹出后,登录以上的布局(包括登录)向上移动到输入法之上,并且,背景不会被压缩。
如下所示:
如果想让弹出的输入法,保证界面背景图不被压缩,那么一定要使用adjustPan属性。
看如下图所示的分析:
一个简单的模型,当输入法弹出的时候,可能登录按钮已经被遮盖住,那么我们只需要让登录按钮以上的布局通过scrollTo方式上移他被遮盖的距离就行了。
下面开始贴代码,代码很简单,先是布局文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/root" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:gravity="center_vertical" android:background="@drawable/update_empty"> <EditText android:layout_width="fill_parent" android:layout_height="50dip" android:hint="edit1"/> <EditText android:layout_width="fill_parent" android:layout_height="50dip" android:hint="edit2"/> <EditText android:layout_width="fill_parent" android:layout_height="50dip" android:hint="edit3"/> <Button android:id="@+id/submit" android:layout_width="fill_parent" android:layout_height="50dip" android:text="submit"/></LinearLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
接下来就是ManiActivity里的代码了,里面写了很详尽的注释,上面也解释了一些:
package com.example.myandrtest;import android.app.Activity;import android.graphics.Rect;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.ViewTreeObserver.OnGlobalLayoutListener;import android.widget.Button;import android.widget.LinearLayout;public class MainActivity extends Activity { private LinearLayout linearLayout; private Button mSubmit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_third); linearLayout = (LinearLayout) findViewById(R.id.root); mSubmit = (Button) findViewById(R.id.submit); controlKeyboardLayout(linearLayout, mSubmit); } /** * @param root * 最外层布局,需要调整的布局 * @param scrollToView * 被键盘遮挡的scrollToView,滚动root,使scrollToView在root可视区域的底部 */ private void controlKeyboardLayout(final View root, final View scrollToView) { // 注册一个回调函数,当在一个视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变时调用这个回调函数。 root.getViewTreeObserver().addOnGlobalLayoutListener( new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { Rect rect = new Rect(); // 获取root在窗体的可视区域 root.getWindowVisibleDisplayFrame(rect); // 当前视图最外层的高度减去现在所看到的视图的最底部的y坐标 int rootInvisibleHeight = root.getRootView() .getHeight() - rect.bottom; Log.i("tag", "最外层的高度" + root.getRootView().getHeight()); // 若rootInvisibleHeight高度大于100,则说明当前视图上移了,说明软键盘弹出了 if (rootInvisibleHeight > 100) { //软键盘弹出来的时候 int[] location = new int[2]; // 获取scrollToView在窗体的坐标 scrollToView.getLocationInWindow(location); // 计算root滚动高度,使scrollToView在可见区域的底部 int srollHeight = (location[1] + scrollToView .getHeight()) - rect.bottom; root.scrollTo(0, srollHeight); } else { // 软键盘没有弹出来的时候 root.scrollTo(0, 0); } } }); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
同时,Manifest里面的activity里,一定要是 android:windowSoftInputMode=”adjustPan”
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myandrtest" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="15" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" android:windowSoftInputMode="adjustPan" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
本文章参考文章地址为:http://www.cnblogs.com/kobe8/p/4030412.html
- Android输入法弹出,布局上移,背景不会压缩
- Android输入法弹出,布局上移,背景不会压缩
- Android输入法弹出,布局上移,背景不会压缩
- Android 输入法框弹出 背景上移或压缩问题
- Android layout实现输入法弹出后,布局整体上移
- Android layout实现输入法弹出后,布局整体上移
- Android 输入法弹出界面上移的问题
- android 软键盘弹出 布局上移动
- Android 解决沉浸式状态栏下,输入法弹出,布局不会自动调整的BUG
- 关于Android弹出输入法后页面需要上移的问题。
- Android软键盘弹出,界面布局整体上移
- Android弹出软键盘布局是否上移问题
- Android 盘弹出时布局内指定内容上移
- 软键盘弹出布局上移
- Android 异常 弹出输入法 布局改变
- 弹出输入法键盘,标题栏上移消失问题解决
- 当输入法调出时,如何让界面自动上移,使输入法不会遮挡到主界面 android:windowSoftInputMode属性使用
- Android界面技巧:当输入法调出时,如何让界面自动上移,使输入法不会遮挡到主界面(Activity)
- J2EE项目代码编写规范分享
- 2017-3-16信息技术笔记
- [bigdata054] python3 urllib的timeout测试
- Android状态栏透明
- dos 命令
- Android输入法弹出,布局上移,背景不会压缩
- 悄悄上谷歌 不要干坏事
- leetcode 82. Remove Duplicates from Sorted List II
- 手游服务器开发技术详解
- 机器学习语料,深度学习语料,神经网络语料,nlp语料,机器视觉语料,持续更新
- quick cocos2dx clipNode详解
- 编程之鸡兔同笼问题
- AUC原理介绍及求解方法总结
- 采用axis框架,进行webservice接口开发、调用时,http://127.0.0.1:8080/TestService/services/TestService?wsdl即wsdl的xml文件