下面方法只适应于没有设置全屏模式 。设置全屏模式则无效
方式一:简单 但是按钮一定要在最下方,局限性很大
将layout的最外层用 RelativeLayout
将需要软键盘跟随的控件放置在RelativeLayout的底部
让其他控件的位置根据这个控件来设置相对值
android:windowSoftInputMode=“adjustResize”
方式二:外部套ScrollView
布局文件如下
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/sc_view"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:background="#ff0000"
android:title="标题"
android:layout_height="55dp">
</android.support.v7.widget.Toolbar>
<EditText
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/editText2"
android:layout_marginTop="300dp"
/>
<Button android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="链接"
android:layout_below="@+id/editText2"
android:id="@+id/button_link"
android:layout_weight="1"
/>
</RelativeLayout>
</ScrollView>
代码如下
scView = (ScrollView) findViewById(R.id.sc_view);
EditText usernamelogin_username = (EditText) findViewById(R.id.editText2);
usernamelogin_username.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
changeScrollView();
return false;
}
});
/**
* 使ScrollView指向底部
*/
private void changeScrollView() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
scView.scrollTo(0, scView.getHeight());
}
}, 300);
}
方法三:设置全屏模式后 软键盘弹出 不仅按钮没被顶起 EditText 都不会顶起
main为 根据局 Id 和 scroll为遮挡按钮 Id
public void addLayoutListener(final View main, final View scroll) {
main.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect rect = new Rect();
main.getWindowVisibleDisplayFrame(rect);
int mainInvisibleHeight = main.getRootView().getHeight() - rect.bottom;
if (mainInvisibleHeight > 100) {
int[] location = new int[2];
scroll.getLocationInWindow(location);
int srollHeight = (location[1] + scroll.getHeight()) - rect.bottom;
main.scrollTo(0, srollHeight);
} else {
main.scrollTo(0, 0);
}
}
});
}
如果 设置全屏模式后 软键盘弹出 仅为解决EditText 不会顶起的问题 则拷贝如下代码
/*
* 解决键盘遮住输入框
* */
public class AndroidBug5497Workaround {
// For more information, see https://issuetracker.google.com/issues/36911528
// To use this class, simply invoke assistActivity() on an Activity that already has its content view set.
public static void assistActivity (Activity activity) {
new AndroidBug5497Workaround(activity);
}
private View mChildOfContent;
private int usableHeightPrevious;
private FrameLayout.LayoutParams frameLayoutParams;
private AndroidBug5497Workaround(Activity activity) {
FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
mChildOfContent = content.getChildAt(0);
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
possiblyResizeChildOfContent();
}
});
frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
}
private void possiblyResizeChildOfContent() {
int usableHeightNow = computeUsableHeight();
if (usableHeightNow != usableHeightPrevious) {
int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
int heightDifference = usableHeightSansKeyboard - usableHeightNow;
if (heightDifference > (usableHeightSansKeyboard/4)) {
// keyboard probably just became visible
frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
} else {
// keyboard probably just became hidden
frameLayoutParams.height = usableHeightSansKeyboard;
}
mChildOfContent.requestLayout();
usableHeightPrevious = usableHeightNow;
}
}
private int computeUsableHeight() {
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
return (r.bottom - r.top);
}
}