【问题标题】:How to keep the toolbar visible with adjustPan?如何使用 adjustPan 保持工具栏可见?
【发布时间】:2016-06-18 05:25:06
【问题描述】:

我有一个类似下面的布局,当用户点击 EditText 时我希望有以下行为:

  1. 键盘应该覆盖“LinearLayout对齐到底部”;
  2. EditText 应该在屏幕上可见,而不是被键盘覆盖;
  3. 工具栏应该在屏幕上可见;

我的布局

预期行为

windowSoftInputMode = adjustPan 的实际结果

未满足要求:

  1. 工具栏应该在屏幕上可见;

windowSoftInputMode = adjustResize 的实际结果

未满足要求:

  1. 键盘应该覆盖“LinearLayout对齐到底部”;

windowSoftInputMode = adjustNothing 的实际结果

未满足要求:

  1. EditText 应该在屏幕上可见,而不是被键盘覆盖;

有没有人遇到过同样的问题并达到了要求?

【问题讨论】:

  • 找到解决方案了吗?
  • 尚未找到开箱即用的解决方案
  • Android 又是一个让人头疼的问题……你找到解决方案了吗?
  • 这是全屏模式还是有状态栏?

标签: android layout keyboard android-toolbar


【解决方案1】:

我不确定是否有任何开箱即用的解决方案可以使其按预期工作。

但是,您可以通过使用windowSoftInputMode = adjustNothing 来实现指定的行为 并设置焦点侦听器以编辑文本,并在编辑文本获得焦点时滚动滚动视图。

如果不清楚如何实现滚动,我可以添加一个示例。

【讨论】:

  • 感谢您的回复,这很清楚,但我正在寻找一个开箱即用的解决方案,如果有的话。同时会接受你的回答。
【解决方案2】:

我认为没有非常简单的方法可以实现这一点。但是有一个项目here 有一个SizeNotifierRelativeLayout,然后在键盘打开时弹出。

【讨论】:

    【解决方案3】:

    将父布局改为相对布局

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

    您的 Activity 或 Fragment 的下一个包含此代码

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

    【讨论】:

      【解决方案4】:

      我执行以下操作来解决此问题。

      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">
      

      【讨论】:

      • adjustResize -> 没有动画
      【解决方案5】:

      试试这个。它会将您的编辑文本滚动到焦点模式并为其打开键盘。

      EditText editText = (EditText) findViewById(R.id.myTextViewId);
      editText.requestFocus();
      InputMethodManager imm = (InputMethodManager) 
      getSystemService(Context.INPUT_METHOD_SERVICE);
      imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多