zoukankan      html  css  js  c++  java
  • 微信输入文字和更多切换时不改变下面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就是软键盘的高度了。

  • 相关阅读:
    收集的正则表达式
    全面解析JavaScript中“&&”和“||”操作符(总结篇)
    3.5 二叉查找树的几何应用
    3.4 散列表
    3.3 平衡查找树
    3.2 符号表之二叉查找树BST
    3.1 符号表之二分查找
    2.7 二叉堆及优先队列
    2.6 经典排序算法总结
    2.5 3-way quickSort
  • 原文地址:https://www.cnblogs.com/visuals/p/4977329.html
Copyright © 2011-2022 走看看