50

根据材料设计规范,当键盘出现时,BottomNavigationView应该隐藏在它下面。但是,如果我android:windowSoftInputMode="adjustResize"在 Activity 的清单中设置,那么BottomNavigationView键盘上方的移动。

我需要设置adjustResize为在键盘打开时启用滚动到屏幕底部。但是,我不希望BottomNavigationView可见。这可以做到吗?

它目前的外观:

在此处输入图像描述

布局 XML(实际上会有一个is 和 the 的FrameLayout位置):EditTextEditText

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Input"
        android:layout_gravity="center"
        android:layout_centerVertical="true"/>

    <android.support.design.widget.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:itemBackground="@color/colorPrimary"
        app:menu="@menu/menu_bottom_navigation"
        app:itemIconTint="@android:color/white"
        app:itemTextColor="@android:color/white"/>

</RelativeLayout>
4

5 回答 5

53

将此添加到清单中的活动中

android:windowSoftInputMode="adjustPan"

所以喜欢

<activity android:name=".feature.home.HomeActivity" 
 android:windowSoftInputMode="adjustPan"/>
于 2017-07-05T02:46:18.877 回答
6

解决方案(或另一种方法)

我经历过与OP所说的完全相同的情况,我BottomNavigationView显然在屏幕底部有一个,在屏幕上方有一个ScrollView

现在,如果我们adjustPan在活动中进行,那么BottomNavigationView当键盘出现但滚动不起作用时,它会保持在底部。

如果我们这样做了,adjustResize那么滚动就可以了,但是 BottomNavigationView 会被推到键盘上。

我认为下面可以是相同的两种方法。

方法一

只需在键盘显示/隐藏上将可见性设置为消失/可见。这是相同的快速解决方法。您可以在下一个方法中获取键盘隐藏/显示事件的侦听器。

为了让它看起来有趣,您可以尝试使用某种动画来显示/隐藏 BottomNavigationView。

方法二

一些更好的方法(材料设计方法)将是使用CoordinatorLayout和滚动行为(与您可能已经看到的相同CollapsingToolBar)。

下面是布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:title="@string/title"
            app:titleTextColor="@android:color/white" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nestedScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

    ------ Your Contents --------

    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
        app:menu="@menu/navigation" />
</android.support.design.widget.CoordinatorLayout>

就是这样,现在您可以看到 BottomNavigationView 在滚动到底部和顶部等时隐藏/显示。但是在一种情况下,您可能会遇到另一个问题,即当键盘被隐藏时,如果内容太小而无法滚动,

问题是,当键盘打开并且您滚动到隐藏BottomNavigationView的底部时,现在如果您按下后退按钮键盘隐藏但BottomNavigationView仍然隐藏。现在,由于内容不可滚动,因此如果您尝试滚动,它不会显示 BottomNavigationView。要再次显示它,您需要做的是,再次使键盘可见,然后向上滚动,当显示 BottomNavigationView 时,然后按返回按钮。

我试图以这种方式解决这个问题,

添加全局侦听器以查看键盘是显示还是隐藏。我在这里使用的代码是(它在 Kotlin 中,但是如果需要,您可以轻松地将其转换为 Java 版本)

private fun addKeyboardDetectListener(){
    val topView = window.decorView.findViewById<View>(android.R.id.content)
    topView.viewTreeObserver.addOnGlobalLayoutListener {
        val heightDifference = topView.rootView.height - topView.height
        if(heightDifference > dpToPx(this, 200F)){
            // keyboard shown
            Log.d(TAG, "keyboard shown")
        } else {
            // keyboard hidden
            Log.d(TAG, "keyboard hidden")
            val behavior = (navigation.layoutParams as CoordinatorLayout.LayoutParams).behavior as HideBottomViewOnScrollBehavior
            behavior.slideUp(navigation)
        }
    }
}

fun dpToPx(context: Context, valueInDp: Float) : Float{
    val displayMetrics = context.resources.displayMetrics
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, valueInDp, displayMetrics)
}

最后一件事,如果您使用支持库版本 28.0.0,那么您将看到该 behavior.slideUp(navigation)方法受到保护,因此您不能从您的活动等中调用它。

然而,谷歌的 Android 团队已经在新的material-components. 选中此项,只需在您的项目中导入材料组件并改用此类。

除此之外,您还可以尝试更多实验,例如在键盘隐藏/显示上以编程方式调用 slideUp 或 slideDown 等。

PS我花了很多时间来了解这种完全有效的方法,所以想在这里分享它,这样可以节省别人的时间。

于 2019-03-23T14:35:05.880 回答
0

还有另一种解决方案,它不需要adjustSpan,但它仅适用于API >= 21. 您可以通过跟踪系统插图来检测键盘是否显示/隐藏。假设您有BottomNavigationView,它是的子项LinearLayout,您需要在显示键盘时隐藏它:

> LinearLayout
  > ContentView
  > BottomNavigationView

您需要做的就是以LinearLayout这种方式扩展:

public class KeyboardAwareLinearLayout extends LinearLayout {
    public KeyboardAwareLinearLayout(Context context) {
        super(context);
    }

    public KeyboardAwareLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public KeyboardAwareLinearLayout(Context context,
                                     @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public KeyboardAwareLinearLayout(Context context, AttributeSet attrs,
                                     int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public WindowInsets onApplyWindowInsets(WindowInsets insets) {
        int childCount = getChildCount();
        for (int index = 0; index < childCount; index++) {
            View view = getChildAt(index);
            if (view instanceof BottomNavigationView) {
                int bottom = insets.getSystemWindowInsetBottom();
                if (bottom >= ViewUtils.dpToPx(200)) {
                    view.setVisibility(GONE);
                } else {
                    view.setVisibility(VISIBLE);
                }
            }
        }
        return insets;
    }
}

这个想法是,当显示键盘时,系统插图会以相当大的.bottom价值发生变化。

于 2018-02-15T18:37:09.147 回答
-2

有时从其他布局复制粘贴会发生这种情况。在我的情况下,我只是 tools:context="Your class name"从布局文件中删除。谢谢

于 2021-02-02T03:07:44.883 回答
-4

作为与您的替代方式,您android:windowSoftInputMode="adjustResize"可以尝试这个。

从您的调用此方法OnCreate- 一旦键盘启动,您可以更改不需要显示的视图的可见性!当键盘按下时再次显示它们。

 public void checkKeyBoardUp(){
        rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                rootView.getWindowVisibleDisplayFrame(r);
                int heightDiff = rootView.getRootView().getHeight() - (r.bottom - r.top);

                if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
                    //ok now we know the keyboard is up...
                    whatEverView.setVisibility(View.INVISIBLE);

                }else{
                    //ok now we know the keyboard is down...
                    whatEverView.setVisibility(View.VISIBLE);
              }
            }
        });
    }
于 2017-02-05T12:29:27.570 回答