Android 软键盘顶起布局相关

41 篇文章 0 订阅

原文链接:http://blog.csdn.net/mr_liu_gege/article/details/53169359
拾人牙慧,感谢博主分享。试验了一下,确实有效果,收藏学习。

第一种情况:被键盘遮住的按钮没有位于布局的底部

代码:


 
 
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width= "match_parent"
  4. android:layout_height= "wrap_content"
  5. android:orientation= "vertical"
  6. android:id= "@+id/parent_ll">
  7. <EditText
  8. android:id= "@+id/username"
  9. android:layout_width= "match_parent"
  10. android:layout_height= "wrap_content"
  11. android:layout_marginTop= "200dp"
  12. android:ems= "10" >
  13. </EditText>
  14. <EditText
  15. android:id= "@+id/userpwd"
  16. android:layout_width= "match_parent"
  17. android:layout_height= "wrap_content"
  18. android:layout_marginTop= "30dp"
  19. android:ems= "10"
  20. android:inputType= "textPassword" />
  21. <Button
  22. android:id= "@+id/btn"
  23. android:layout_width= "wrap_content"
  24. android:layout_height= "wrap_content"
  25. android:layout_gravity= "center"
  26. android:layout_marginTop= "30dp"
  27. android:text= "Button" />
  28. </LinearLayout>

 
 
  1. public class MainActivity extends Activity {
  2. private Button btn;
  3. private LinearLayout parent_ll;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.layout_login);
  8. /*ScrollView mScrollView = (ScrollView)findViewById(R.id.scrollContent);
  9. mScrollView.setVerticalScrollBarEnabled(false);
  10. mScrollView.setHorizontalScrollBarEnabled(false);*/
  11. parent_ll=(LinearLayout) findViewById(R.id.parent_ll);
  12. btn=(Button) findViewById(R.id.btn);
  13. addLayoutListener(parent_ll, btn);
  14. }
  15. /**
  16. * 1、获取parentView在窗体的可视区域
  17. * 2、获取parentView在窗体的不可视区域高度
  18. * 3、判断不可视区域高度
  19. * 1、大于100:键盘显示 获取childView的窗体坐标
  20. * 算出parentView需要滚动的高度,使childView显示。
  21. * 2、小于100:键盘隐藏
  22. *
  23. * @param parentView 根布局
  24. * @param childView 需要显示的最下方View
  25. */
  26. public void addLayoutListener(final View parentView, final View childView) {
  27. parentView.getViewTreeObserver().addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() {
  28. @Override
  29. public void onGlobalLayout() {
  30. Rect rect = new Rect();
  31. parentView.getWindowVisibleDisplayFrame(rect);
  32. int mainInvisibleHeight = parentView.getRootView().getHeight() - rect.bottom;
  33. if (mainInvisibleHeight > 100) {
  34. int[] location = new int[ 2];
  35. childView.getLocationInWindow(location);
  36. int srollHeight = (location[ 1] + childView.getHeight()) - rect.bottom;
  37. parentView.scrollTo( 0, srollHeight);
  38. } else {
  39. parentView.scrollTo( 0, 0);
  40. }
  41. }
  42. });
  43. }
  44. }
addLayoutListener() 这个方法是关键。

第二种情况:被键盘遮住的按钮位于布局的底部

父布局需要是Relativelayout,按钮需要alignParentBottom,可能清单文件还需要设置android:windowSoftInputMode

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值