軟鍵盤彈出頂起佈局的小技巧

在安卓開發中我們會很頻繁的和軟鍵盤打交道,但是軟鍵盤本來是不屬於我們的佈局的,它的出現會遮擋佈局,比如,佈局中有一個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、將想要頂起的佈局居於底部(保證此部分佈局被全部頂起);

做到這三點就可以簡單實現軟鍵盤彈出,將佈局頂起,且不擠壓視頻播放的效果了,本人文筆不好,且技術比較糙,希望寫下的東西能幫助到大家!也希望大家多提意見!謝謝!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章