溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么讓軟鍵盤彈出時,部分控件上移

發(fā)布時間:2020-07-20 02:00:44 來源:網(wǎng)絡 閱讀:3699 作者:駿駿Daniel 欄目:移動開發(fā)

之前寫注冊頁面的時候,UI同學給我提了個意見,讓彈出軟鍵盤時候,左上角的標題“注冊”不動,中間內(nèi)容往上移動,效果這樣
怎么讓軟鍵盤彈出時,部分控件上移
怎么讓軟鍵盤彈出時,部分控件上移
經(jīng)過查閱資料和多方實踐,解決方法如下

1、先要設置頁面軟鍵盤模式,這樣每次軟鍵盤彈出后布局高度會減少軟鍵盤的高度

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN
                | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

2、找出內(nèi)容view,在每次布局為軟鍵盤彈出減小高度時監(jiān)聽,改變內(nèi)容view

mVContent = findViewById(android.R.id.content);

ViewTreeObserver.OnGlobalLayoutListener mLsnrLayout =
            new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout () {

                    Rect rect = new Rect();
                    mVContent.getWindowVisibleDisplayFrame(rect);
                    int height = rect.height();

                    boolean isImmShow = height < UcApp.sHeightPx;
                    if ( isImmShow != mIsImmShow ) {
                        mIsImmShow = isImmShow;

                        mUbtnCommit.setVisibility(isImmShow ? View.INVISIBLE : View.VISIBLE);
                    }
                }
            };

mVContent.getViewTreeObserver().addOnGlobalLayoutListener(mLsnrLayout);

XML格式是這樣的,標題我是在基類上設置加上的
怎么讓軟鍵盤彈出時,部分控件上移

    <!--注冊布局-->
    <android.support.constraint.ConstraintLayout
        android:id="@+id/cl_register"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:visibility="visible">
public void setTitle (String txt) {

    if ( null == mTvTitle ) {

        mTvTitle = new TextView(UcApp.sCtx);
        ((FrameLayout) getWindow().getDecorView()).addView(mTvTitle);
        ViewUtils.inst(mTvTitle)
                .setMargins(119, 54, 0, 0)
                .setTextSize(42);
        mTvTitle.setTypeface(Typeface.DEFAULT_BOLD);
        mTvTitle.setTextColor(ComUtils.getClr(R.color.white_f2f2f2_60));
    }

    mTvTitle.setText(txt);
}

就是設置了margin

因為內(nèi)容布局是豎直居中的,標題是margin寫死的,所以布局高度變了以后內(nèi)容移動標題不動。

希望這篇文章能幫助大家,大佬勿噴~

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI