Android layout实现输入法弹出后,布局整体上移

2018-06-02 00:31:27

今天在给手机设置PIN码时,发现在设置PIN码的页面,输入框和底部的按钮会随着输入法的弹出而上移,从而不至于被输入法挡住。 
这样的布局是怎么实现的呢?经过尝试,我也实现了同样的效果。下面来进行分析。

先分别看下输入法未弹出和弹出后的效果: 
 

下面看具体实现:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="20sp"
        android:paddingTop="30dp"
        android:text="请输入密码:"/>

    <LinearLayout        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:gravity="center_vertical">

        <ScrollView            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText                android:id="@+id/gridview"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:hint="edit here"
                android:inputType="textPassword"
                android:gravity="center"/>

        </ScrollView>
    </LinearLayout>

    <LinearLayout        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:dividerPadding="10dp">

        <Button            android:id="@+id/smit_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="取 消" />

        <Button            android:id="@+id/cancel_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="确 定" />"    </LinearLayout></LinearLayout>1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859

整个布局由三部分组成: 
顶部是一个TextView,文字为“请输入密码”,底部是一个Linearlayout,包含两个Button。这两部分的高度是固定的。 
其余部分是一个Linearlayout,高度为0dp并且其layout_weight为1,这个属性是一个关键,表示这个Linearlayout将会填充除了其余两部分之外的所有屏幕空间。在这个Linearlayout里,输入框被一个ScrollView包含,这个ScrollView是另一个关键,如果不用ScrollView的话,中间部分就不会上移,输入法弹出后就会遮挡底部按钮。

注意

经过测试,下面几种情况下,这样布局上移的效果会失效: 
1.在Manifest里给activity加上android:windowSoftInputMode=”adjustPan”这样的属性。 
2.实现了沉浸式状态栏,比如在setContentView之前加上这些:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);}1234

那如果要实现沉浸式状态栏又要保持布局不会被输入法遮挡,怎么办呢? 
只要在根布局加上android:fitsSystemWindows=”true”即可,效果如下(为方便看效果我把背景改成了黄色): 


  • 2019-08-06 15:30:08

    小程序展示富文本

    然而rich-text有个问题,它不能够给内联img设置宽度100%,这样图片就容易溢出屏幕。我们只能在后台返回富文本的时候对图片的img标签进行格式化。或者前端进行格式化也行,我赞成使用后端,因为很多种情况我们不一定都能想得到。

  • 2019-08-07 09:07:32

    最全的Service Worker讲解

    Service Worker 最主要的特点是:在页面中注册并安装成功后,运行于浏览器后台,不受页面刷新的影响,可以监听和截拦作用域范围内所有页面的 HTTP 请求。 基于 Service Worker API 的特性,结合 Fetch API、Cache API、Push API、postMessage API 和 Notification API,可以在基于浏览器的 web 应用中实现如离线缓存、消息推送、静默更新等 native 应用常见的功能,以给 web 应用提供更好更丰富的使用体验。

  • 2019-08-07 09:09:19

    windows系统下定时关闭程序

    其中xxx.exe是你要关闭的进程中运行的exe,可以ctrl+alt+del打开任务管理器,进到详细信息查看 然后把.txt文件后缀改成.bat(此时要在查看一栏勾上文件拓展名,要不还是txt文档)

  • 2019-08-07 09:16:43

    一个比较完美的PWA例子

    但就目前来讲,PWA是Google主推的一项技术标准,FireFox,Chrome以及一些基于Blink的浏览器已经支持渐进式Web应用了,Edge上对渐进式Web应用的支持还在开发。Apple公司也表示会考虑在自己Safari支持PWA。然而这项功能已经进入了WebKit内核的五年计划中。长期来看,对浏览器兼容性的支持方面应该已经不算太大问题了。况且在现阶段,在不支持渐进式Web应用的浏览器中,你的应用也只是无法使用渐进式Web应用的离线功能而已,除此之外的功能均可以正常使用。

  • 2019-08-07 09:57:48

    spring data jpa 实体类中字段不与数据库表映射

    当我们使用spring data jpa开发的时候,会将实体类中的成员变量与表中的字段一一对应,当我们在实体类中加上一个不与数据库表一一对应的成员变量的时候,此时我们只要在这个成员变量上加上注解@Transient @

  • 2019-08-07 17:16:53

    如何在 Node.js 中使用 import / export 的三种方法

    因为一些历史原因,虽然 Node.js 已经实现了 99% 的 ES6 新特性,不过截止 2018.8.10,How To Enable ES6 Imports in Node.JS 仍然是老大难问题,下面我来介绍三种方法可以让我们在 Node.js 中使用 import/export 。

  • 2019-08-13 08:56:46

    nuxtjs组合element

    添加elementUI 插件,plugins->ele.js,代码如下