軟鍵盤擋住

當(dāng)軟鍵盤出現(xiàn)遮擋住布局后,如果修改成SOFT_INPUT_ADJUST_PANSOFT_INPUT_ADJUST_RESIZE依然解決不了想要的需求,那么下面這個(gè)就是最合適解決軟鍵盤擋住的方案了。
解決方案大概要述:把布局修改成滑動(dòng)式布局,當(dāng)出現(xiàn)軟鍵盤后,將最外面的布局高度縮至軟鍵盤上面,并且整體布局依然可以保持滑動(dòng)。

對本身代碼侵略性也少,一句代碼放在Activity即可。

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ```
        Softkeyboard.assistActivity(rlContent);
        ```
    }

布局

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/rlContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include layout="@layout/newui_title_bar" />

        <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/title_layout"
            android:animateLayoutChanges="true"
            android:orientation="vertical">

        </ScrollView>

    </RelativeLayout>

Softkeyboard

public class Softkeyboard {

    // For more information, see https://issuetracker.google.com/issues/36911528
    // To use this class, simply invoke assistActivity() on an Activity that already has its content view set.

    public static void assistActivity(Activity activity) {
        new Softkeyboard(activity);
    }

    public static void assistActivity(RelativeLayout rlContent) {
        new Softkeyboard(rlContent);
    }

    private View mChildOfContent;
    private int usableHeightPrevious;
    private FrameLayout.LayoutParams frameLayoutParams;

    private Softkeyboard(Activity activity) {
        FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
        mChildOfContent = content.getChildAt(0);
        mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(() -> possiblyResizeChildOfContent());
        frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
    }

    private Softkeyboard(RelativeLayout rlContent) {
        mChildOfContent = rlContent;
        mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(() -> possiblyResizeChildOfContent());
        frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
    }

    private void possiblyResizeChildOfContent() {
        int usableHeightNow = computeUsableHeight();
        if (usableHeightNow != usableHeightPrevious) {
            int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
            int heightDifference = usableHeightSansKeyboard - usableHeightNow;
            if (heightDifference > (usableHeightSansKeyboard / 5)) {
                // keyboard probably just became visible
                frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
            } else {
                // keyboard probably just became hidden
                frameLayoutParams.height = usableHeightSansKeyboard;
            }
            mChildOfContent.requestLayout();
            usableHeightPrevious = usableHeightNow;
        }
    }

    private int computeUsableHeight() {
        Rect r = new Rect();
        mChildOfContent.getWindowVisibleDisplayFrame(r);
        return (r.bottom - r.top);
    }

}

在這里對Softkeyboard的一段代碼usableHeightSansKeyboard / 5說一下,如果包含導(dǎo)航欄也加入滑動(dòng)布局的話,就 usableHeightSansKeyboard / 4

最后編輯于
?著作權(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ù)。

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