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

今天在给手机设置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>

整个布局由三部分组成:
顶部是一个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);
}

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

  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
实现输入法弹出后,布局整体上移的效果可以通过给根布局设置android:fitsSystemWindows="true"属性来实现。 同,为了使顶部部分固定,可以将顶部部分放在一个固定高度的布局中,将中间部分放在一个可滚动的布局中。当输入法弹出,中间部分的布局会被上移,而顶部部分则不会受到影响。 以下是一个示例布局文件: ``` <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <!-- 顶部部分,设置固定高度 --> <RelativeLayout android:id="@+id/top_layout" android:layout_width="match_parent" android:layout_height="60dp" android:background="@color/colorPrimary"/> <!-- 中间部分,放在可滚动的布局中 --> <ScrollView android:layout_below="@id/top_layout" android:layout_above="@+id/bottom_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- 中间部分的内容 --> </LinearLayout> </ScrollView> <!-- 底部部分,放置输入框等控件 --> <RelativeLayout android:id="@+id/bottom_layout" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- 底部部分的控件 --> </RelativeLayout> </RelativeLayout> ``` 注意,这只是一个示例布局,具体实现还需要根据实际需求进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值