Android输入法弹出,布局上移,背景不会压缩
来源:互联网 发布:泉州招聘 福建网络 编辑:程序博客网 时间:2024/06/06 05:44
转载请注明出处,谢谢: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>
接下来就是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); } } }); }}
同时,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>
本文章参考文章地址为: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)
- Mxnet学习系列1----制作自己的数据
- iOS---自动释放池
- 关于蛇形填数问题求解
- Nginxs学习-1安装Nginx 将Nginx作为静态网页服务器
- python中调用matlab函数
- Android输入法弹出,布局上移,背景不会压缩
- Windows 10驱动签名_win 10驱动数字签名_驱动签名注意事项
- 【OpenCV】透视变换-将图像由不规则四边形转换成矩形
- 复习了一下NGUI
- iOS每日一记——————————三方库。。。。
- google地图项目总结
- 判断移动端打开的是ios还是android还是微信浏览器
- Data Vault初探(九) —— 定期装载_Kettle_附属表
- Java数据库连接 - 预编译SQL语句