Android输入法弹出,布局上移,背景不会压缩

转载请注明出处,谢谢: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

  • 17
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 27
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 27
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值