我在一些应用程序中看到当软键盘展示时布局会发生变化。这肯定不是 adjustPan
,因为整个布局(可能是内部布局)都会移动,而不仅仅是当前的 EditText
。例如,在 Evernote 登录屏幕中就有此现象。请问如何实现这种效果?
我在一些应用程序中看到当软键盘展示时布局会发生变化。这肯定不是 adjustPan
,因为整个布局(可能是内部布局)都会移动,而不仅仅是当前的 EditText
。例如,在 Evernote 登录屏幕中就有此现象。请问如何实现这种效果?
这里有一个类似Evernote登录界面的解决方案:
首先,定义一个类,并将其设置为您特殊的LinearLayout类型,如下所示:
public class MyLayout extends LinearLayout {
public MyLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyLayout(Context context) {
super(context);
}
private OnSoftKeyboardListener onSoftKeyboardListener;
@Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
if (onSoftKeyboardListener != null) {
final int newSpec = MeasureSpec.getSize(heightMeasureSpec);
final int oldSpec = getMeasuredHeight();
if (oldSpec > newSpec){
onSoftKeyboardListener.onShown();
} else {
onSoftKeyboardListener.onHidden();
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public final void setOnSoftKeyboardListener(final OnSoftKeyboardListener listener) {
this.onSoftKeyboardListener = listener;
}
public interface OnSoftKeyboardListener {
public void onShown();
public void onHidden();
}
}
该布局会监听大小的变化,如果新的测量值小于旧的测量值,那就意味着键盘挡住了屏幕的一部分。
然而,为了使其正常工作,在您的清单中需要设置android:windowSoftInputMode="adjustResize"
,这样内容将被调整大小而不仅仅是移动位置。
整个系统的工作方式如下:
<MyLayout id="layout">
<SomeImage id="image"/>
<SomeText>
<SomeInput>
</MyLayout>
它就像 Evernote 的登录界面。 然后,在您的活动中:
((MyLayout)findViewById(R.id.layout)).setOnSoftKeyboardListener(new OnSoftKeyboardListener() {
@Override
public void onShown() {
findViewById(R.id.image).setVisibility(View.GONE);
}
@Override
public void onHidden() {
findViewById(R.id.image).setVisibility(View.VISIBLE);
}
});
然后转到manifest.xml并设置
android:windowSoftInputMode="adjustResize"
当软键盘弹出时,会隐藏图片并调整其余内容的大小。(您实际上可以在Evernote中看到文本如何被调整)
隐藏图像当然是众多可行方案之一。但是必须小心,因为不同的布局更改也将调用onMeasure。
当然,这是一个不太优雅的变体。您需要检查方向更改,并在实际测量时间和比较新规格与旧规格时可能需要一些额外的逻辑。但我认为这是唯一的方法。
在AndroidManifest.xml中使用"adjustResize"代替"adjustPan"
<activity
...
android:windowSoftInputMode="adjustResize" />
根据文档:
"adjustResize"
无论何时软键盘出现,activity的主窗口都会被调整大小以腾出空间。"adjustPan"
activity的主窗口不会被调整大小来为软键盘腾出空间。相反,窗口的内容会自动平移,以确保当前焦点不被键盘遮挡,用户可以随时看到他们正在输入什么内容。通常情况下调整大小比这种方式更可取,因为用户可能需要关闭软键盘才能访问和与窗口中被遮盖的部分进行交互。
所选答案在全屏模式下无效。以下一行解决方案适用于我:
AndroidBug5497Workaround.assistActivity(this);
从以下链接中复制AndroidBug5497Workaround类:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
在父布局中使用滚动视图,并从清单文件中删除adjustpan或adjustresize设置。 在您的布局中使用ScrollView将为您提供免费的功能,可以滚动您的面板或布局,并保护您的布局不被工具栏或操作栏覆盖,也不会隐藏布局内的按钮或文本字段。请记住,您必须在基本布局中使用ScrollView,因此此ScrollView将作为整个屏幕的基本布局。 示例
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- Add here which you want -->
</LinearLayout>
</ScrollView>
adjustPan
选项,并且当软键盘显示时,界面将向上移动以显示您当前输入文本的editText,这是完全可以的。我的问题是如何使整个布局不可见,而只显示活动的editText(s)。 - Eugene