软键盘弹出顶起布局的小技巧

在安卓开发中我们会很频繁的和软键盘打交道,但是软键盘本来是不属于我们的布局的,它的出现会遮挡布局,比如,布局中有一个EditText是位于底部的,点击之后弹出软键盘,如果我们不做任何处理,那软键盘必然会将EditText遮挡,这是很糟糕的效果,该怎么避免呢?

首先我想到了Activity的windowSoftInputMode属性,这个属性能影响两件事情:
1、当有焦点产生时,软键盘是隐藏还是显示
2、是否改变活动主窗口大小以便腾出空间展示软键盘
它有以下值可以设置:
1、stateUnspecified:软键盘的状态并没有指定,系统将选择一个合适的状态或依赖于主题的设置
2、stateUnchanged:当这个activity出现时,软键盘将一直保持在上一个activity里的状态,无论是隐藏还是显示
3、stateHidden:用户选择activity时,软键盘总是被隐藏
4、stateAlwaysHidden:当该Activity主窗口获取焦点时,软键盘也总是被隐藏的
5、stateVisible:软键盘通常是可见的
6、stateAlwaysVisible:用户选择activity时,软键盘总是显示的状态
7、adjustUnspecified:默认设置,通常由系统自行决定是隐藏还是显示
8、adjustResize:该Activity总是调整屏幕的大小以便留出软键盘的空间
9、adjustPan:当前窗口的内容将自动移动以便当前焦点从不被键盘覆盖,并且用户总能看到输入内容的部分
可以设置一个或多个,多个之间用|分开。这些值中符合我们要求的是adjustResize和adjustPan。先看adjustPan,它会将当前获取了焦点的EditText之上的布局整体上移,以此来达到EditText不被软键盘覆盖的目的。但如果我只想让EditText和与EditText有关的一些控件上移,而它们之上的控件保持不变呢?OK,这时候我们就需要用到adjustResize,但是光用它还是不够的,还需要我们的布局配合。

我的需求如下:
1、布局的上半部分放一个视频播放器,可以实现简单的视频播放功能;
2、布局的下半部分有包括EditText在内的布局
3、点击EditText,要求包括EditText在内的布局均被软键盘顶起,而视频播放的布局不被挤压

实现效果如下
这里写图片描述

本人技术粗糙,布局写的不太好,但基本效果算是实现了(这里有用到点击EditText外部,隐藏键盘的小技巧,想要理解的请移步我的另一篇博客http://blog.csdn.net/k_bb_666/article/details/78805971),还有需要优化的地方,希望各位看官多提意见!直接上代码吧!
1、在清单文件中给相应的Activity设置windowSoftInputMode属性

        <activity
            android:name=".activity.VideoPublishActivity"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustResize|stateHidden" />

2、布局文件

<?xml version="1.0" encoding="utf-8"?>
//最外层必须是RelativeLayout,才能保证布局被顶起
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    tools:context="com.daishu.copperman.activity.VideoPublishActivity">
    //软键盘弹出时,这个布局不动,且视频播放正常,不被挤压变形
    //这层套FrameLayout是为了防止播放视频的布局被挤压
    <FrameLayout 
        android:id="@+id/fl_video_play"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rl_title">
        //我在这里动态添加了播放器
        <RelativeLayout 
            android:id="@+id/rl_video_play"
            android:layout_width="match_parent"
            android:layout_height="360dp"
            android:background="#f2f2f2">
        </RelativeLayout>
    </FrameLayout>
    //以下是需要被软键盘顶起的布局
    <LinearLayout
        android:id="@+id/ll_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" //想要顶起的布局必须居于底部
        android:background="#fff"
        android:orientation="vertical">

        <EditText
            android:id="@+id/et_publish_title"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@null"
            android:gravity="start"
            android:hint="请输入标题(30字以内)" />

        <com.zhy.view.flowlayout.TagFlowLayout
            android:id="@+id/id_flowlayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="15dp"
            zhy:max_select="1" />

        <Button
            android:id="@+id/btn_publish"
            style="?android:attr/borderlessButtonStyle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:background="@drawable/shape_publish_btn"
            android:gravity="center"
            android:text="发布"
            android:textColor="#2e2e2e"
            android:textSize="14sp" />
    </LinearLayout>
 </RelativeLayout>

我们总结一下(敲黑板!划重点!)
1、最外层布局必须是相对布局RelativeLayout(保证只顶起部分布局,而不是将整个界面上移);
2、需要在承载视频播放器的布局外套一层FrameLayout(保证布局顶起时视频不被挤压);
3、将想要顶起的布局居于底部(保证此部分布局被全部顶起);

做到这三点就可以简单实现软键盘弹出,将布局顶起,且不挤压视频播放的效果了,本人文笔不好,且技术比较糙,希望写下的东西能帮助到大家!也希望大家多提意见!谢谢!

猜你喜欢

转载自blog.csdn.net/k_bb_666/article/details/78544738