【问题标题】:Make FAB respond to Soft Keyboard show/hide changes使 FAB 响应软键盘显示/隐藏更改
【发布时间】:2016-02-18 05:26:15
【问题描述】:

我看过各种关于 FAB 响应屏幕底部的 Snackbar 弹出窗口以及滚动敏感 FAB 的帖子。但是有FloatingActionButton.Behavior(或类似的)的一些实现在FAB弹出时将它移到键盘上方

现在,当我单击 EditText 框时,键盘会覆盖 FAB。我的目标是为 FAB 设置动画,使其始终可见,与键盘状态无关。

编辑:android:windowSoftInputMode="adjustResize"...="adjustPan" 都不会改变任何东西。好吧,adjustResize 调整了底层布局(在我的例子中是地图)的大小,但 FAB 没有移动。

【问题讨论】:

  • 已经遇到过。 OP 使用了没有CoordinatorLayout 的 FAB 的自定义实现。更改windowSoftInputMode(如答案中所建议)也无济于事,正如我在编辑中所写的那样。
  • 你弄明白了吗?
  • 不,仍在等待答案

标签: android floating-action-button


【解决方案1】:

我遇到了同样的问题,我尝试在我的根视图中添加一个ScrollView,但它不起作用,因为内容没有超过显示器的高度(在这种情况下,工厂的反应如预期)。

所以我尝试了android:windowSoftInputMode="adjustResize",它对我有用。

有关信息,这是我的布局 xml 的样子:

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

    <ManyViewsExceptScrollViews/>

    <android.support.design.widget.FloatingActionButton
        [..]
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"/>

</RelativeLayout>

这是一个在活动中膨胀的Fragment(在FrameLayout 中,match_parent 是根视图)我在清单中添加了android:windowSoftInputMode="adjustResize"

【讨论】:

    【解决方案2】:

    您好,我知道它很旧,但对于未来或当前的读者/搜索者以及线程制作者,他还没有找到答案。这就是我在我的应用中出现这种行为的方式。

    Fab 隐藏在 RecyclerView 滚动上,当小吃栏弹出时上升,如果未显示 fab 并且小吃栏弹出,如果您滚动,那么 Fab 仍将显示在小吃栏顶部,当 SB 消失时将向下移动并持续键盘如果打开 Fab 将被向上推。 (对不起,我不得不写因为我不知道如何用 eclipse 模拟器给 gif)

    图片

    布局

    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout_drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/GrayGood"
    android:clipToPadding="false"
    android:fitsSystemWindows="true" >
    
    <android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/layout_coordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/layout_appLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark" >
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways|snap"
                android:background="?attr/colorPrimary" />
        </android.support.design.widget.AppBarLayout>
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerViewMain"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:clipToPadding="false" />
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/floating_action_button_margin"
            app:layout_behavior="com.faisal.cvcd.anim.ScrollingFABAnimation"
            android:src="@drawable/ic_fab"
            android:tint="@color/White"
            app:backgroundTint="@color/colorPrimary"
            app:borderWidth="0dp"
            app:elevation="6dp"
            app:fabSize="normal" />
     </android.support.design.widget.CoordinatorLayout>
    </android.support.v4.widget.DrawerLayout>
    

    如您所见,我正在使用 FabAnimation 类来覆盖它的一些默认方法。

    滚动FAB动画

    import android.content.Context;
    import android.support.design.widget.CoordinatorLayout;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v4.view.ViewCompat;
    import android.util.AttributeSet;
    import android.view.View;
    
    public class ScrollingFABAnimation extends
            CoordinatorLayout.Behavior<FloatingActionButton> {
    
    public ScrollingFABAnimation(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    //For SnackBar to push up the Floating Button when it pops up
    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent,    FloatingActionButton child, View dependency) {
        return dependency instanceof Snackbar.SnackbarLayout;
    }
    
    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child, View dependency) {
        float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
        child.setTranslationY(translationY);
        return true;
    }
    
    //For FAB to hide or show on scroll
    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View directTargetChild, View target,
            int nestedScrollAxes) {
        return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL  || super.onStartNestedScroll(coordinatorLayout, child,
                        directTargetChild, target, nestedScrollAxes);
    }
    
    @Override
    public void onNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View target, int dxConsumed,
            int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed,  dyConsumed, dxUnconsumed, dyUnconsumed);
    
        if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
            child.hide();
        } else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
            child.show();
        }
     }
    }
    

    【讨论】:

    • 我没有设置 Android 开发环境,因此无法测试您的解决方案,抱歉。但是谢谢发布它!
    • 这种向上移动的协调器布局行为适用于 SnackBar,而不是软键盘。
    猜你喜欢
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 2019-12-21
    • 1970-01-01
    相关资源
    最近更新 更多