Android软键盘顶起整个布局的调整

本文介绍了在Android应用中如何通过配置Activity的android:windowSoftInputMode属性来避免EditText弹起软键盘时遮挡顶部UI控件的问题,实现了类似QQ聊天界面的效果。

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

设计Android布局的时候,如果在UI的底部有EditText控件的时候,当EditText在当前activity获得焦点的时候,弹起系统软键盘会把这个EditEext之上的整块布局往上面顶起,这样的效果肯定不是我们想要的,我们想要的是效果是跟QQ聊天界面一样,当软键盘弹起的时候只是压缩中间聊天内容的块,不影响顶部的UI控件!具体实现我们看下面介绍:


1、在AndroidManifest.xml中配置我们的Activity,在activity中设置android:windowSoftInputMode软键盘属性

              

android:windowSoftInputMode="adjustResize"

android:windowSoftInputMode="adjustResize|stateHidden"

adjustResize是允许重新调整布局,只设置adjustResize属性,当acitvity激活是,Editext会自动获取焦点弹起软键盘,如果我们要不需要马上弹起软键盘,只要再加上stateHidden就可以了!

chat_head

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <Button
            android:id="@+id/chat_pub"
            android:layout_width="40dip"
            android:layout_height="40dip"
            android:textSize="18sp"
            android:layout_marginLeft="2dip"
            android:text="公"/>

        <Button
            android:id="@+id/chat_sys"
            android:layout_width="40dip"
            android:layout_height="40dip"
            android:textSize="18sp"
            android:text="系"
            android:layout_marginLeft="2dip"
             />
        
        <Button
            android:id="@+id/chat_fri"
            android:layout_width="40dip"
            android:layout_height="40dip"
            android:textSize="18sp"
            android:layout_marginLeft="2dip"
            android:text="友"
             />
        
        <Button
            android:id="@+id/chat_sen"
            android:layout_width="40dip"
            android:layout_height="40dip"
            android:textSize="18sp"
            android:layout_marginLeft="2dip"
            android:text="私"
             />
        
        <Button
            android:id="@+id/chat_bang"
            android:layout_width="40dip"
            android:layout_height="40dip"
            android:textSize="18sp"
            android:layout_marginLeft="2dip"
            android:text="帮"
             />
        
        <Button
            android:id="@+id/chat_zu"
            android:layout_width="40dip"
            android:layout_height="40dip"
            android:textSize="18sp"
            android:layout_marginLeft="2dip"
            android:text="队"
             />
        
        <Button
            android:id="@+id/chat_wor"
            android:layout_width="40dip"
            android:layout_height="40dip"
            android:textSize="18sp"
            android:layout_marginLeft="2dip"
            android:text="世"
             />

</LinearLayout>

chat_bot

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <EditText
            android:id="@+id/chat_edi"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="6"
            android:ems="9" >
        </EditText>

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="Button" />

        <Button
            android:id="@+id/chat_sendmsg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="发送" />

</LinearLayout>

main

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <include layout="@layout/chat_head" />
    <android.support.v4.view.ViewPager 
        android:id="@+id/myViewPager"
        android:layout_width="fill_parent"
        android:layout_height="0.0dip"
        android:layout_gravity="center"
        android:layout_weight="10">
        
    </android.support.v4.view.ViewPager>
    <include layout="@layout/chat_bot"/>


</LinearLayout>

我们只是看效果,java代码就不做任何数据的处理了,直接运行项目就可以看到我们要的效果了,可以把AndroidManifest.xml中android:windowSoftInputMode去掉再看看效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值