android 當設置Activity狀態欄爲透明時,鍵盤彈出ScrollView滾動無效解決方案

final View decorView = getWindow().getDecorView();
decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        Rect rect = new Rect();
        decorView.getWindowVisibleDisplayFrame(rect);
        int screenHeight = decorView.getRootView().getHeight();
        int heightDifference = screenHeight - rect.bottom;//計算軟鍵盤佔有的高度  = 屏幕高度 - 視圖可見高度
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) loginBinding.scrollView.getLayoutParams();
        layoutParams.setMargins(0, 0, 0, heightDifference);//設置ScrollView的marginBottom的值爲軟鍵盤佔有的高度即可
        loginBinding.scrollView.requestLayout();
    }
});
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章