最近遇到的一个问题,基类里面有toolbar,子类里面recyclerView/scrollView里面item的edittext 软键盘被遮挡;
测试了,没有加toolbar的activity没有问题,不会遮挡;
toolbar直接加在activity里面,没问题,不会遮挡;
但是,toolbar放基类里面,就会出现问题。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical"> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="visible"> <ImageView android:id="@+id/iv_bg_bar" android:layout_width="match_parent" android:layout_height="48dp" android:background="@color/toolbar" android:scaleType="centerCrop"/> <android.support.v7.widget.Toolbar android:id="@+id/tool_bar" android:layout_width="match_parent" android:layout_height="48dp" android:layout_alignParentStart="true" android:layout_alignParentTop="true" app:popupTheme="@style/AppTheme.PopupOverlay"> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:text="全屏" android:textColor="#FFFFFF" android:textSize="18sp" android:textStyle="bold"/> </android.support.v7.widget.Toolbar> </RelativeLayout> <FrameLayout android:id="@+id/base_containner" android:layout_width="match_parent" android:layout_height="match_parent"> </FrameLayout> </LinearLayout>
解决:
在基类或者子类的oncreate里面加:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
SOFT_INPUT_ADJUST_PAN 是调整窗口视图,避免遮挡; SOFT_INPUT_STATE_ALWAYS_HIDDEN 是防止一进来activity就弹出软键盘的.
/** Adjustment option for {@link #softInputMode}: set to have a window * pan when an input method is * shown, so it doesn't need to deal with resizing but just panned * by the framework to ensure the current input focus is visible. This * can <em>not</em> be combined with {@link #SOFT_INPUT_ADJUST_RESIZE}; if * neither of these are set, then the system will try to pick one or * the other depending on the contents of the window. */要加在oncreate里面才可以,在manifest里面加这个属性没有作用
<activity android:name="***" android:configChanges="keyboard" android:screenOrientation="portrait" android:windowSoftInputMode="stateVisible|adjustResize"/>
一个博客:
如何解决软键盘弹出引起的各种不适
http://unicorn25.iteye.com/blog/916504