android.view.ViewGroup
protected void onLayout(boolean changed, int l, int t, int r, int b)
执行layout操作时调用onLayout方法。View要给它的每个Child设定size和position。拥有Children的子类需要重写onLayout方法并且调用每个Child的layout方法。
参数changed表示view的size或position发生变化。参数l, t, r, b分别表示相对于parent的left, top, right, bottom position。
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
测量View及其Content,确定measuredWidth和measuredHeight。在方法measure(int, int)中调用。重写onMeasure方法时,需要调用方法setMeasuredDimension(int, int),存储View的measuredWidth和measuredHeight。若存储失败,方法measure(int, int)会抛出异常IllegalStateException。可以调用super.onMeasure(int, int)方法。
除非MeasureSpec准许更大的size,否则measure的默认实现是background size。子类重写onMeasure(int, int)提供Content的更佳测量。如果onMeasure被重写,子类必须保证measuredWidth和measuredHeight至少是view的minHeight和minWidth。minHeight/Width通过getSuggestedMinimumHight/Width()获取。
参数width/heightMeasureSpec表示parent强加的horizontal/vertical space要求。
void android.view.ViewGroup.layout(int l, int t, int r, int b)
给view及其descendants设定size和position。它正是layout机制的第2个步,每个parent调用它的chlidren的layout操作。使用在measure阶段测量得到的size和position数据完成layout操作。拥有child的子类必须重写onLayout方法,调用每个child的layout操作。
void android.view.ViewGroup.measureChildren(int widthMeasureSpec, int heightMeasureSpec)
请View的所有children测量themseles, 测量依据是View的MeasureSpec和padding。忽略处于GONE状态的children,是否GONE状态由getChildMeasureSpec来确定。
参数width/heightMeasureSpec表示view的width/height要求。
使用自定义布局,页面布局中包含ScrollVIew,在软键盘弹起后,布局的高度会发生改变,根据布局的高度来判断软键盘的状态。
1. package com.ransj.keyboard;
2.
3. import android.content.Context;
4. import android.util.AttributeSet;
5. import android.util.Log;
6. import android.widget.RelativeLayout;
7.
8. public class KeyboardLayout extends RelativeLayout {
9. private static final String TAG = KeyboardLayout.class.getSimpleName();
10. public static final byte KEYBOARD_STATE_SHOW = -3;
11. public static final byte KEYBOARD_STATE_HIDE = -2;
12. public static final byte KEYBOARD_STATE_INIT = -1;
13. private boolean mHasInit;
14. private boolean mHasKeybord;
15. private int mHeight;
16. private onKybdsChangeListener mListener;
17.
18. public KeyboardLayout(Context context, AttributeSet attrs, int defStyle) {
19. super(context, attrs, defStyle);
20. }
21.
22. public KeyboardLayout(Context context, AttributeSet attrs) {
23. super(context, attrs);
24. }
25.
26. public KeyboardLayout(Context context) {
27. super(context);
28. }
29. /**
30. * set keyboard state listener
31. */
32. public void setOnkbdStateListener(onKybdsChangeListener listener){
33. mListener = listener;
34. }
35. @Override
36. protected void onLayout(boolean changed, int l, int t, int r, int b) {
37. super.onLayout(changed, l, t, r, b);
38. if (!mHasInit) {
39. mHasInit = true;
40. mHeight = b;
41. if (mListener != null) {
42. mListener.onKeyBoardStateChange(KEYBOARD_STATE_INIT);
43. }
44. } else {
45. mHeight = mHeight < b ? b : mHeight;
46. }
47. if (mHasInit && mHeight > b) {
48. mHasKeybord = true;
49. if (mListener != null) {
50. mListener.onKeyBoardStateChange(KEYBOARD_STATE_SHOW);
51. }
52. Log.w(TAG, "show keyboard.......");
53. }
54. if (mHasInit && mHasKeybord && mHeight == b) {
55. mHasKeybord = false;
56. if (mListener != null) {
57. mListener.onKeyBoardStateChange(KEYBOARD_STATE_HIDE);
58. }
59. Log.w(TAG, "hide keyboard.......");
60. }
61. }
62.
63. public interface onKybdsChangeListener{
64. public void onKeyBoardStateChange(int state);
65. }
66. }
复制代码
这个是自定义的布局,自定义布局可以继承各种常见布局。自定义布局有键盘状态改变监听器,可以通过注册监听器来监听软键盘状态。
1. <?xml version="1.0" encoding="utf-8"?>
2. <com.ransj.keyboard.KeyboardLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:id="@+id/keyboardLayout1"
4. android:layout_width="fill_parent"
5. android:layout_height="fill_parent" >
6.
7. <ScrollView
8. android:layout_width="fill_parent"
9. android:layout_height="fill_parent"
10. android:layout_alignParentLeft="true"
11. android:layout_alignParentTop="true"
12. android:fillViewport="true" >
13.
14. <LinearLayout
15. android:layout_width="fill_parent"
16. android:layout_height="wrap_content"
17. android:orientation="vertical" >
18.
19. <TextView
20. android:id="@+id/testone_tv"
21. android:layout_width="fill_parent"
22. android:layout_height="wrap_content"
23. android:background="#000000"
24. android:gravity="center"
25. android:text="软件盘弹起,我将消失!软键盘隐藏,我将回来!"
26. android:layout_weight="1.0"
27. android:textColor="#00ff00"
28. android:textStyle="bold" />
29.
30. <EditText
31. android:id="@+id/testone_et"
32. android:layout_width="fill_parent"
33. android:layout_height="wrap_content"></EditText>
34. </LinearLayout>
35. </ScrollView>
36.
37. </com.ransj.keyboard.KeyboardLayout>
复制代码
这个是优化页面的布局,从上面可以看出自定义布局的用法。
1. package com.ransj.keyboard;
2.
3. import com.ransj.keyboard.KeyboardLayout.onKybdsChangeListener;
4.
5. import android.app.Activity;
6. import android.os.Bundle;
7. import android.view.View;
8. import android.widget.TextView;
9. import android.widget.Toast;
10. /**
11. *
12. *
13. */
14. public class TestOne extends Activity{
15.
16. @Override
17. protected void onCreate(Bundle savedInstanceState) {
18. super.onCreate(savedInstanceState);
19. setContentView(R.layout.testone);
20. KeyboardLayout mainView = (KeyboardLayout) findViewById(R.id.keyboardLayout1);
21. final TextView tv = (TextView) findViewById(R.id.testone_tv);
22. mainView.setOnkbdStateListener(new onKybdsChangeListener() {
23.
24. public void onKeyBoardStateChange(int state) {
25. switch (state) {
26. case KeyboardLayout.KEYBOARD_STATE_HIDE:
27. tv.setVisibility(View.VISIBLE);
28. Toast.makeText(getApplicationContext(), "软键盘隐藏", Toast.LENGTH_SHORT).show();
29. break;
30. case KeyboardLayout.KEYBOARD_STATE_SHOW:
31. tv.setVisibility(View.INVISIBLE);
32. Toast.makeText(getApplicationContext(), "软键盘弹起", Toast.LENGTH_SHORT).show();
33. break;
34. }
35. }
36. });
37. }
38.
39. @Override
40. protected void onDestroy() {
41. super.onDestroy();
42. }
43.
44. @Override
45. protected void onPause() {
46. super.onPause();
47. }
48.
49. }
复制代码
这个是优化页面的代码逻辑,通过注册监听器,来监听软键盘事件。在软键盘弹起时隐藏Textview,在软键盘隐藏时显示Textview。
msg_send_view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int total_msg_height=msg_send_view.getRootView().getHeight()-msg_send_view.getHeight();
LogHelper.info("******************************2布局高度:"+currentHeight);//获取当前短信布局高度
LogHelper.info("******************************2屏幕高度:"+globleHeight);//获取当前短信布局高度
LogHelper.info("******************************2适配器高度:"+msgHeight);//获取当前短信布局高度
msgHeight=mPullRefreshListView.getMeasuredHeight();
if(total_msg_height>100){
if(currentHeight>msgHeight){
actualListView.setStackFromBottom(true);
}
else{
actualListView.setStackFromBottom(false);
}
}else{
if(currentHeight>globleHeight){
actualListView.setStackFromBottom(true);
}
else{
actualListView.setStackFromBottom(false);
}
}
}
});
}