有一点很重要:如果只设置:在manifest中要设置android:windowSoftInputMode="adjustPan",会造成bottom.xml布局被挡住一小部分。
所以用下面的方法,再加上:在manifest中要设置android:windowSoftInputMode="adjustPan",就可以达到如下效果:listview最底下一条item的时候会把listview顶上去,其他情况,会把当前位置顶上去
以下方法引子别人博客
这里采用滚动布局来解决输入法遮盖布局的问题,方法如下:
/**
* @param root 最外层布局,需要调整的布局
* @param scrollToView 被键盘遮挡的scrollToView,滚动root,使scrollToView在root可视区域的底部
*/
private void controlKeyboardLayout(final View root, final View scrollToView) {
root.getViewTreeObserver().addOnGlobalLayoutListener( new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect rect = new Rect();
//获取root在窗体的可视区域
root.getWindowVisibleDisplayFrame(rect);
//获取root在窗体的不可视区域高度(被其他View遮挡的区域高度)
int rootInvisibleHeight = root.getRootView().getHeight() - rect.bottom;
//若不可视区域高度大于100,则键盘显示
if (rootInvisibleHeight > 100) {
int[] location = new int[2];
//获取scrollToView在窗体的坐标
scrollToView.getLocationInWindow(location);
//计算root滚动高度,使scrollToView在可见区域的底部
int srollHeight = (location[1] + scrollToView.getHeight()) - rect.bottom;
root.scrollTo(0, srollHeight);
} else {
//键盘隐藏
root.scrollTo(0, 0);
}
}
});
}
效果图如下:
下面提供完整的代码及布局文件:
1. MainActivity
- public class MainActivity extends Activity {
- private LinearLayout mRoot;
- private Button mSubmit;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mRoot = (LinearLayout) findViewById(R.id.root);
- mSubmit = (Button) findViewById(R.id.submit);
- controlKeyboardLayout(mRoot, mSubmit);
- }
- /**
- * @param root 最外层布局,需要调整的布局
- * @param scrollToView 被键盘遮挡的scrollToView,滚动root,使scrollToView在root可视区域的底部
- */
- private void controlKeyboardLayout(final View root, final View scrollToView) {
- root.getViewTreeObserver().addOnGlobalLayoutListener( new OnGlobalLayoutListener() {
- @Override
- public void onGlobalLayout() {
- Rect rect = new Rect();
- //获取root在窗体的可视区域
- root.getWindowVisibleDisplayFrame(rect);
- //获取root在窗体的不可视区域高度(被其他View遮挡的区域高度)
- int rootInvisibleHeight = root.getRootView().getHeight() - rect.bottom;
- //若不可视区域高度大于100,则键盘显示
- if (rootInvisibleHeight > 100) {
- int[] location = new int[2];
- //获取scrollToView在窗体的坐标
- scrollToView.getLocationInWindow(location);
- //计算root滚动高度,使scrollToView在可见区域
- int srollHeight = (location[1] + scrollToView.getHeight()) - rect.bottom;
- root.scrollTo(0, srollHeight);
- } else {
- //键盘隐藏
- root.scrollTo(0, 0);
- }
- }
- });
- }
- }
2. activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/root"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- android:gravity="center_vertical" >
- <EditText android:layout_width="fill_parent"
- android:layout_height="50dip"
- android:hint="edit1"/>
- <EditText android:layout_width="fill_parent"
- android:layout_height="50dip"
- android:hint="edit2"/>
- <EditText android:layout_width="fill_parent"
- android:layout_height="50dip"
- android:hint="edit3"/>
- <Button android:id="@+id/submit"
- android:layout_width="fill_parent"
- android:layout_height="50dip"
- android:text="submit"/>
- </LinearLayout>
经测试 这种办法可以解决输入法遮挡问题,还可以自己扩展不一定要滚动也可以 在弹出输入法时隐藏一些控件 达到比较好的效果
转自:http://blog.csdn.net/yqichang/article/details/11705235
---------------------------------------
目前新思路:自己计算来滚动界面,基本可行,没有进行大规模适配测试
——————————————————————
最后欢迎关注我的微信公众号:云端看大地
——————————————————————
最后欢迎关注我的微信公众号:云端看大地