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”即可,效果如下(为方便看效果我把背景改成了黄色): 


  • 2017-11-28 14:53:05

    Vagrant 实战

    Vagrant 是一个可创建轻量级、高复用性和便于移植的开发环境的工具。 此文章是作者折腾vagrant的笔记, 希望大家看后,不再去网上搜罗资料, 能顺利搭建vagrant环境。vagrant更新较快, 还建议大家以官网为主。Vagrant官网

  • 2017-11-28 14:55:13

    使用 Vagrant 打造跨平台开发环境

    Vagrant 是一款用来构建虚拟开发环境的工具,非常适合 php/python/ruby/java 这类语言开发 web 应用,“代码在我机子上运行没有问题”这种说辞将成为历史。

  • 2017-11-28 14:57:19

    vagrant up 失败解决办法

    直接使用VirtualBox开启一个vm也会失败,基本上可以确定是VirtualBox版本的问题 有遇到过安装了VirtualBox-5.0.22-108108-Win.exe的版本在win7下用不了,卸载重装VirtualBox-4.3.12-93733-Win.exe之后可用。

  • 2017-12-05 22:30:02

    php7.0升级php7.2

    看电脑上的教程要备份7.0配置文件以及扩展啥的,我感觉不如卸载干净重新安装

  • 2017-12-06 09:35:10

    分页优化的四种方式

    在大数据量的情况下,原本很简单的分页如果没有处理好,你会发现分页的请求会消耗你大量的数据库时间。如果你遇到了这个问题,文章给了你几个很好的解决的方案。当然,初学者若能看完这篇文章,那么它会指导你写出更具有扩展性的分页代码。