解决聊天页面解决软键盘遮挡住输入框,和输入法把整个页面顶上去的问题

本文详细介绍了解决Android应用中键盘遮挡UI元素的问题,包括配置Activity、调整布局及使用自定义LinearLayout的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这是我的效果图

在这里插入图片描述在这里插入图片描述

下面讲一下配置步骤

1.清单文件Activity中里配置

android:windowSoftInputMode="adjustResize"

在这里插入图片描述

2.根目录layout布局 加入 android:fitsSystemWindows=“true”

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fitsSystemWindows="true"
    >

3.Recyview或Listview 设置属性

<ListView
    android:id="@+id/robot_lv"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:divider="@null"
    android:listSelector="@android:color/transparent"
    android:transcriptMode="alwaysScroll" >
</ListView>

下面附上我的完整布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activitys.RobotActivity">
<RelativeLayout
    android:id="@+id/relat_bar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/dp_60"
    android:background="@color/colorBlack">
    <TextView
        android:id="@+id/txt_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginTop="@dimen/dp_10"
        android:gravity="center"
        android:text="@string/title"
        android:textColor="@color/colorWhite"
        android:textSize="20sp" />

    <ImageView
        android:id="@+id/iv_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="@dimen/dp_20"
        android:layout_marginTop="@dimen/dp_10"
        android:scaleType="centerCrop"
        android:src="@drawable/base_back_left" />
</RelativeLayout>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fitsSystemWindows="true"
    >
<!-- transcriptMode 自动向下滚动    alwaysScroll一直向下滚动状态;   divider设置间隔线效果 ;   listSelector设置没有滑动效果 -->
   <com.scwang.smartrefresh.layout.SmartRefreshLayout
   android:id="@+id/refreshLayout"
   android:layout_width="match_parent"
   android:layout_height="0dp"
   android:layout_weight="1">
<ListView
    android:id="@+id/robot_lv"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:divider="@null"
    android:listSelector="@android:color/transparent"
    android:transcriptMode="alwaysScroll" >
</ListView>
  </com.scwang.smartrefresh.layout.SmartRefreshLayout>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:background="@color/holo_orange_light"
    android:orientation="horizontal" >
    <EditText
        android:hint="请输入想说的话…"
        android:visibility="gone"
        android:background="@drawable/edittext_bck"
        android:id="@+id/et_sendText"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:layout_margin="7dp"
        android:paddingBottom="7dp"
        android:paddingTop="7dp" />
    <TextView
        android:visibility="visible"
        android:background="@drawable/edittext_bck"
        android:id="@+id/te_sendText"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:layout_margin="7dp"
        android:paddingBottom="7dp"
        android:paddingTop="7dp" />
    <Button
        android:id="@+id/btn_send"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_margin="7dp"
        android:background="@drawable/send_bck"
        android:text="发送" />
</LinearLayout>
</LinearLayout>
</LinearLayout>

在布局里添加android:fitsSystemWindows=“true”后,会多出一块空白,这里贴上解决办法。

1、新建CustomLinearLayout继承LinearLayout,之后替换之前的根布局(ConstraintLayout、RelativeLayout同理)

 /**
 * 作者:G
 * ClassName:CustomLinearLayout
 * 时间:2020/7/30  9:35 AM
* 概述: 解决android:fitsSystemWindows="true"后顶部出现白条
 */
public class CustomLinearLayout extends LinearLayout {
public CustomLinearLayout(Context context) {
    super(context);
}

public CustomLinearLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public CustomLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
protected boolean fitSystemWindows(Rect insets) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        insets.left = 0;
        insets.top = 0;
        insets.right = 0;
    }
    return super.fitSystemWindows(insets);
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT_WATCH)
@Override
public WindowInsets onApplyWindowInsets(WindowInsets insets) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        return super.onApplyWindowInsets(insets.replaceSystemWindowInsets(0, 0, 0, insets.getSystemWindowInsetBottom()));
    } else {
        return insets;
    }
}
}

完美解决!

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值