调整布局时,软键盘上布局、键盘

由网友(各安天涯℡)分享简介:我在某些应用中布局的变化已经看到了在软键盘显示。这当然不是 adjustPan ,因为整个布局(可能是内部的布局)的转变,不仅是当前的EditText 。这是例如在Evernote的登录屏幕。你能指点如何做? I've seen in some applications the layout shifts when...

我在某些应用中布局的变化已经看到了在软键盘显示。这当然不是 adjustPan ,因为整个布局(可能是内部的布局)的转变,不仅是当前的EditText 。这是例如在Evernote的登录屏幕。你能指点如何做?

I've seen in some applications the layout shifts when soft keyboard is shown. This is certainly not adjustPan because the whole layout (probably inner layout) shifts, not only the current EditText. This is for instance in Evernote login screen. Can you advice how this made?

推荐答案

下面是一个解决方案,它的工作原理类似Evernote的登录屏幕:

Here's a solution that works like the Evernote login screen:

首先,定义一个类,将成为你的特殊的LinearLayout是这样的:

First, define a class that will be your special LinearLayout like this:

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();
}

}

此布局听来测算,如果新的测量<比旧的,这意味着在屏幕的部分由软键盘吃掉。

This layout listens to measure changes, and if new measurements are < than the old ones, that means part of the screen is eaten by soft keyboard.

虽然,它的工作,在你的清单,你需要设置的android:windowSoftInputMode =adjustResize这样的内容将被调整,而不仅仅是移动

Though, for it to work, in your manifest you need to set android:windowSoftInputMode="adjustResize" so the content will be resized and not just shifted.

和整个系统的工作原理如下: 你有你的布局:

And the whole system works as follows: You have your layout:

<MyLayout id="layout">
  <SomeImage id="image"/>
  <SomeText>
  <SomeInput>
</MyLayout>

这就像evernotes登录屏幕。 然后,在你的活动:

It's like evernotes login screen. Then, in your activity:

((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和设置

Then go to manifest.xml and set

android:windowSoftInputMode="adjustResize"

会发生什么,如果是软键盘显示,它会隐藏图像和将调整内容的其余部分。 (实际上,你可以看到文本大小在Evernote的)

What will happen, is when soft keyboard is shown, it'll hide the image and will resize the rest of content. (You can actually see how text is resized in Evernote)

图像隐藏,当然,对很多事情可以做的。但是你一定要小心,因为不同布局的变化也将调用onMeasure。

Image hide is, of course, one of the many things you can do. But you must be careful, since different layout changes will also call onMeasure.

当然,这是一个肮脏的变种。您需要检查的方向变化,正确的时间比较新规范与旧的时候,当采取实际测量,也许一些更多的逻辑。但我认为这是做的唯一途径。

Of course it's a dirty variant. You need to check for orientation changes, and the right time when actually take the measurements, and maybe some more logic when comparing the new specs with the old ones. But i think this is the only way to do it.

阅读全文

相关推荐

最新文章