微信输入文字和更多切换时不改变下面layout大小

首先在Manifest中找到该Activity,设置属性:android:windowSoftInputMode="adjustResize|stateAlwaysHidden"
在XML文件中搞一个自定义Layout如:
<cn.example.view.TestSoftKeyboard
android:id="@+id/test_view"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
然后定义这个Layout:
public class TestSoftKeyboard extends LinearLayout{

private Listener mListener;

    public void setListener(Listener listener){
        this.mListener = listener;
    }

    public interface Listener {
        public void onSoftKeyboardShown(boolean isShowing, int softKeyboardHeight);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height = MeasureSpec.getSize(heightMeasureSpec);
        Rect rect = new Rect();
        Activity activity = (Activity)getContext();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        int statusBarHeight = rect.top;
        int screenHeight = activity.getWindowManager().getDefaultDisplay().getHeight();
        int diff = (screenHeight - statusBarHeight) - height;
        if(mListener != null){
            mListener.onSoftKeyboardShown(diff>128, diff);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
最后在Activity中:
public class TestActivity extends Activity implements cn.example.TestSoftKeyboard.Listener{
……
mTestView = (TestView) findViewById(R.id.test_view);
mTestView .setListener(this);
……
    @Override
    public void onSoftKeyboardShown(boolean isShowing, int softKeyboardHeight) {
        if (isShowing) {
            mAfterMeasureSize = softKeyboardHeight;
            Log.i("Test", "OnSoftKeyboardShown showing softKeyboardHeight :" + softKeyboardHeight);
        } else {
            mPreMeasureSize = softKeyboardHeight;
            Log.i("Test", "OnSoftKeyboardShown dismiss softKeyboardHeight :" + softKeyboardHeight);
        }
        if(mAfterMeasureSize - mPreMeasureSize > 150){
           int h = mAfterMeasureSize - mPreMeasureSize;
        }
    }

}
上面的h就是软键盘的高度了,如果你的Activity设置了Title,记得用h这个值,否则,在TestSoftKeyboard这个类中得到的diff就是软键盘的高度了。

原文地址:https://www.cnblogs.com/visuals/p/4977329.html