码迷,mamicode.com
首页 > 微信 > 详细

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

时间:2015-11-19 14:44:15      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:

首先在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就是软键盘的高度了。

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

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!