Android 如何使用adjustPan保持工具栏可见?
我有一个如下所示的布局,当用户点击EditText时,我希望有以下行为:Android 如何使用adjustPan保持工具栏可见?,android,layout,keyboard,android-toolbar,Android,Layout,Keyboard,Android Toolbar,我有一个如下所示的布局,当用户点击EditText时,我希望有以下行为: 键盘应覆盖“与底部对齐的线性布局” 编辑文本应在屏幕上可见,而不是被键盘覆盖 工具栏应在屏幕上可见 我的布局 预期行为 WindowsOfInputMode=adjustPan的实际结果 未满足的要求: 工具栏应该在屏幕上可见 WindowsOfInputMode=adjustResize的实际结果 未满足的要求: 键盘应覆盖“与底部对齐的线性布局” WindowsOfInputMode=adjustNothing
是否有人遇到过同样的问题并达到了要求?我不确定是否有现成的解决方案可以让它如您所期望的那样工作 但是,您可以使用
windowSoftInputMode=adjustNothing
以及将焦点侦听器设置为编辑文本,并在编辑文本获得焦点时滚动滚动视图
如果不清楚如何实现滚动,我可以添加一个示例。我认为没有非常简单的方法来实现这一点。但有一个项目有一个相对的布局,当键盘打开时会弹出。将父布局更改为相对布局
public class SizeNotifierRelativeLayout extends RelativeLayout {
private Rect rect = new Rect();
public SizeNotifierRelativeLayoutDelegate delegate;
public abstract interface SizeNotifierRelativeLayoutDelegate {
public abstract void onSizeChanged(int keyboardHeight);
}
public SizeNotifierRelativeLayout(Context context) {
super(context);
}
public SizeNotifierRelativeLayout(android.content.Context context, android.util.AttributeSet attrs) {
super(context, attrs);
}
public SizeNotifierRelativeLayout(android.content.Context context, android.util.AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/**
* Calculate the soft keyboard height and report back to listener
* @param changed
* @param l
* @param t
* @param r
* @param b
*/
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (delegate != null) {
View rootView = this.getRootView();
int usableViewHeight = rootView.getHeight() - AndroidUtilities.statusBarHeight - AndroidUtilities.getViewInset(rootView);
this.getWindowVisibleDisplayFrame(rect);
int keyboardHeight = usableViewHeight - (rect.bottom - rect.top);
delegate.onSizeChanged(keyboardHeight);
}
}
接下来,在您的活动或片段中包括以下代码
private SizeNotifierRelativeLayout sizeNotifierRelativeLayout;
View rootView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rootView = view.findViewById(R.id.root);
sizeNotifierRelativeLayout = (SizeNotifierRelativeLayout)
view.findViewById(R.id.chat_layout);
sizeNotifierRelativeLayout.delegate = this;
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new
ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
rootView.getWindowVisibleDisplayFrame(r);
int screenHeight = rootView.getRootView().getHeight();
int keypadHeight = screenHeight - r.bottom;
Log.d("Height", "keypadHeight = " + keypadHeight);
if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height.
// keyboard is opened
if (sizeNotifierRelativeLayout != null) {
sizeNotifierRelativeLayout.setPadding(0, 0, 0, keypadHeight - 200);
}
} else {
// keyboard is closed
if (sizeNotifierRelativeLayout != null) {
sizeNotifierRelativeLayout.post(new Runnable() {
public void run() {
if (sizeNotifierRelativeLayout != null) {
sizeNotifierRelativeLayout.setPadding(0, 0, 0, 0);
}
}
});
}
try {
}
} catch (NullPointerException e) {
e.printStackTrace();
System.out.print(e.getMessage());
}
}
}
});
}
为解决此问题,我执行了以下操作 AndroidManifest文件活动配置:
<activity
android:name=".chat.app.activity.ChatPageActivity"
android:configChanges="orientation|screenLayout"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
android:windowSoftInputMode="stateAlwaysHidden|adjustResize">
感谢您的回复,这很清楚,但我正在寻找现成的解决方案,如果有的话。我会同时接受你的答案。找到解决方案了吗?还没有找到现成的解决方案另一个让Android头疼的问题…你找到解决方案了吗?调整大小->没有动画