软键盘上推隐藏操作栏或覆盖编辑文本

Soft keyboard push up hidding action bar or overlay edittext

我对简单布局有疑问。 如果我设置

android:windowSoftInputMode="stateHidden|adjustResize"

发生这种情况: Bug

无论如何,只有当我从横向打开键盘,然后旋转(打开键盘)时才会发生这种情况。 在正常使用中(旋转然后打开键盘)不会发生这种情况!

如果我设置

android:windowSoftInputMode="stateHidden|adjustPan"

ActionBar 向上推。

此处布局代码:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/a"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_below="@+id/ad_view_chat"
        android:layout_gravity="bottom"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/listChatLayout"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_above="@+id/switchbuttons"
            android:layout_marginTop="2dp">
            <ListView
                android:id="@+id/listChat"
                android:layout_gravity="bottom"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:longClickable="true"
                android:stackFromBottom="true"
                android:transcriptMode="normal" />
        </LinearLayout>

        <RelativeLayout
            android:id="@+id/switchbuttons"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_above="@+id/messaggiLayout"
            android:layout_centerHorizontal="true">
            <RadioButton
                android:id="@+id/cmd"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="15dp"
                android:checked="false"
                android:gravity="center"
                android:text="Cmd"
                android:layout_marginLeft="5dp" />

            <RadioButton
                android:id="@+id/message"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="25dp"
                android:layout_toRightOf="@+id/cmd"
                android:checked="true"
                android:gravity="center"
                android:text="Message" />

            <com.rey.material.widget.Button
                android:id="@+id/button_arrow_down"
                android:background = "@drawable/arrow_down"
                style="@style/FlatColorButtonRippleStyle"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:text=""
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Button"
                android:textColor="#FF2196F3"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/button_arrow_up"
                android:layout_toEndOf="@+id/button_arrow_up"
                android:layout_marginLeft="3dp" />

            <com.rey.material.widget.Button
                android:id="@+id/button_arrow_up"
                style="@style/FlatColorButtonRippleStyle"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:background = "@drawable/arrow_up"
                android:text=""
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Button"
                android:textColor="#FF2196F3"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/message"
                android:layout_toEndOf="@+id/message"
                android:layout_marginLeft="20dp" />
        </RelativeLayout>


        <RelativeLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/listChatLayout"
            android:layout_alignParentBottom="true"
            android:id="@+id/messaggiLayout">
            <EditText
                android:id="@+id/messaggi"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:imeOptions="actionSend|flagNoFullscreen"
                android:inputType="textImeMultiLine"
                android:layout_toLeftOf="@+id/button_send"
                android:maxLength="100">
                <requestFocus />
            </EditText>
            <com.rey.material.widget.Button
                android:id="@+id/button_send"
                style="@style/FlatColorButtonRippleStyle"
                android:layout_width="50dp"
                android:layout_alignParentRight="true"
                android:background = "@drawable/send2"
                android:layout_height="50dp"
                android:text=""
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Button"
                android:textColor="#FF2196F3" />
        </RelativeLayout>
    </RelativeLayout>

    <com.google.android.gms.ads.AdView
        android:id="@+id/ad_view_chat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/banner_chat_ad_unit_id" />
</RelativeLayout>

修复: 问题出在 mikepenz MaterialDrawer 中。 现在我联系了迈克,我们修复了这个错误

问题出现在SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN标志时,要求显示StatusBar后面的抽屉。

作为解决方案,我实现了 KeyboardUtil,它将键盘的高度作为填充添加到 contentview。由于在显示键盘时没有侦听器获取事件,因此这是通过 OnGlobalLayoutListener 完成的。

@MarcoCount 有一个特殊的用例导致 KeyboardUtil 无法正常工作。在他的帮助下,我们得到了 KeyboardUtil 的更新版本,它现在在屏幕旋转时也能正常工作,键盘一直显示。

这是更新的 KeyboardUtil 的来源,以备不时之需。它也可以在 source of the MaterialDrawer

中的 GitHub 上找到
/*
 * Copyright 2015 Mike Penz All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mikepenz.materialdrawer.util;

import android.app.Activity;
import android.graphics.Rect;
import android.os.Build;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.inputmethod.InputMethodManager;

/**
 * Created by mikepenz on 14.03.15.
 * This class implements a hack to change the layout padding on bottom if the keyboard is shown
 * to allow long lists with editTextViews
 * Basic idea for this solution found here: 
 */
public class KeyboardUtil {
    private View decorView;
    private View contentView;

    public KeyboardUtil(Activity act, View contentView) {
        this.decorView = act.getWindow().getDecorView();
        this.contentView = contentView;

        //only required on newer android versions. it was working on API level 19
        if (Build.VERSION.SDK_INT >= 19) {
            decorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
        }
    }

    public void enable() {
        if (Build.VERSION.SDK_INT >= 19) {
            decorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
        }
    }

    public void disable() {
        if (Build.VERSION.SDK_INT >= 19) {
            decorView.getViewTreeObserver().removeOnGlobalLayoutListener(onGlobalLayoutListener);
        }
    }


    //a small helper to allow showing the editText focus
    ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Rect r = new Rect();
            //r will be populated with the coordinates of your view that area still visible.
            decorView.getWindowVisibleDisplayFrame(r);

            //get screen height and calculate the difference with the useable area from the r
            int height = decorView.getContext().getResources().getDisplayMetrics().heightPixels;
            int diff = height - r.bottom;

            //if it could be a keyboard add the padding to the view
            if (diff != 0) {
                // if the use-able screen height differs from the total screen height we assume that it shows a keyboard now
                //check if the padding is 0 (if yes set the padding for the keyboard)
                if (contentView.getPaddingBottom() != diff) {
                    //set the padding of the contentView for the keyboard
                    contentView.setPadding(0, 0, 0, diff);
                }
            } else {
                //check if the padding is != 0 (if yes reset the padding)
                if (contentView.getPaddingBottom() != 0) {
                    //reset the padding of the contentView
                    contentView.setPadding(0, 0, 0, 0);
                }
            }
        }
    };


    /**
     * Helper to hide the keyboard
     *
     * @param act
     */
    public static void hideKeyboard(Activity act) {
        if (act != null && act.getCurrentFocus() != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) act.getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(act.getCurrentFocus().getWindowToken(), 0);
        }
    }
}

尝试在布局中使用滚动视图,而不是在清单文件中添加调整面板。 使用滚动视图,您的屏幕是可调和可滚动的,这不会向上滚动您的工具栏或操作栏,也不会隐藏您的布局。

<ScrollView>
android:layout_width="match_parent"
android:layout_height="match_parent"
 android:fillViewport="true"
 android:scrollbars = "vertical"
 android:scrollbarStyle="insideInset"
> </ScrollView>

尝试使用此布局作为您的父布局。但是滚动视图不能用作基本布局,因为您必须在线性或相对布局中使用滚动视图。