android軟鍵盤遮擋布局

一、校正鍵盤
方法一:在你的activity中的oncreate中setContentView之前寫上這個(gè)代碼

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

方法二:
在項(xiàng)目的AndroidManifest.xml文件中界面對應(yīng)的<activity>里加入
設(shè)置屬性為 android:windowSoftInputMode="adjustResize" 即可自動(dòng)調(diào)整高度。
設(shè)置屬性為 android:windowSoftInputMode="adjustResize|stateHidden" 即可自動(dòng)調(diào)整高度,且輸入法不自動(dòng)彈出。

二、自定義
1. MainActivity
OnGlobalLayoutListener 是ViewTreeObserver的內(nèi)部類,當(dāng)一個(gè)視圖樹的布局發(fā)生改變時(shí),可以被ViewTreeObserver監(jiān)聽到,這是一個(gè)注冊監(jiān)聽視圖樹的觀察者(observer),在視圖樹的全局事件改變時(shí)得到通知。
總結(jié)為簡單一句話:使用OnGlobalLayoutListener可以監(jiān)聽到布局的變化。
監(jiān)聽到布局變化后,我們就可以自由的操作了:
可以外包一個(gè)ScrollView來進(jìn)行滾動(dòng),使用一個(gè)

scrollView.fullScroll(ScrollView.FOCUS_DOWN)

來滾動(dòng)到底部。
不過,不使用ScrollView也能進(jìn)行滾動(dòng)的,例如LinearLayout也是可以滾動(dòng)的,你還不知道吧,其實(shí),我也是才知道:最基礎(chǔ)的View就有個(gè)ScrollTo()函數(shù)的。
好了,基礎(chǔ)知識準(zhǔn)備好了,剩下就是滾動(dòng)的距離計(jì)算了,我們這樣來計(jì)算:
通過窗體的根View求出總的區(qū)域和可視區(qū)域,這樣就可以計(jì)算出被遮擋的區(qū)域的高度,如果超過一定的值就判斷為軟鍵盤彈出了,然后將根View ScrollTo到一個(gè)位置,將被遮擋的View展示出來。
這里還有個(gè)要注意的地方,就是ScrollTo的參數(shù),先看看函數(shù)原型:

public void scrollTo(int x, int y)

兩個(gè)參數(shù)x、y,是要滾動(dòng)到位置的坐標(biāo),注意,它們是絕對坐標(biāo)。

public class MainActivity extends Activity {

    private int scrollToPosition = 0;
    private LinearLayout mRoot;
    private Button mSubmit;

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRoot = (LinearLayout) findViewById(R.id.root);
        mSubmit = (Button) findViewById(R.id.submit);
        controlKeyboardLayout(mRoot, mSubmit);
    }

    /**
     * @param rootView最外層布局,需要調(diào)整的布局
     * @param scrollToView 被鍵盤遮擋的scrollToView,滾動(dòng)root,使scrollToView在root可視區(qū)域的底部
     */
    private void controlKeyboardLayout(final View rootView, final View scrollToView) {
        rootView.getViewTreeObserver().addOnGlobalLayoutListener( new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect rect = new Rect();
                //獲取rootView在窗體的可視區(qū)域
                rootView.getWindowVisibleDisplayFrame(rect);
                //獲取rootView在窗體的不可視區(qū)域高度(被其他View遮擋的區(qū)域高度)
                int rootInvisibleHeight = rootView.getRootView().getHeight() - rect.bottom;
                //若不可視區(qū)域高度大于100,則鍵盤顯示
                if (rootInvisibleHeight > 100) {
                    int[] location = new int[2];
                    //獲取scrollToView在窗體的坐標(biāo)
                    scrollToView.getLocationInWindow(location);
                    //計(jì)算root滾動(dòng)高度,使scrollToView在可見區(qū)域的底部((location[1] + scrollToView.getHeight()) 為scrollToView
                    // 的bottom)
                    int srollHeight = (location[1] + scrollToView.getHeight()) - rect.bottom;
                    //注意,scrollHeight是一個(gè)相對移動(dòng)距離,而scrollToPosition是一個(gè)絕對移動(dòng)距離
                    scrollToPosition += srollHeight ;
                } else {
                    //鍵盤隱藏
                    scrollToPosition = 0;
                }
                rootView.scrollTo(0, scrollToPosition);
            }
        });
    }

}

2. activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center_vertical" >

    <EditText android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:hint="edit1"/>

    <EditText android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:hint="edit2"/>

    <EditText android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:hint="edit3"/>

    <Button android:id="@+id/submit"
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:text="submit"/>

    </LinearLayout>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容