25

简单的Android软键盘动画

 3 years ago
source link: https://aprildown.xyz/2021/05/28/android-naive-keyboard-animation/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

简单的Android软键盘动画

发表于

2021-05-28 分类于 Android

Disqus: 0 Comments

随着androidx.core1.5.0的发布,我终于搞明白全屏、WindowInsets这些问题了。

  1. 设置windowSoftInputMode

    给Activity增加 android:windowSoftInputMode="adjustResize"

    根据官方文档

    To ensure the best behavior for your app, you should specify how you’d like the system to display your UI in the remaining space.

    而且不同的系统版本,默认的行为不同,所以这里加一个确保行为一致。

    根据官方文档,还可以加一个stateHidden来避免在一些系统版本上一打开Activity,软键盘就弹出的问题。

  2. 全屏/让内容显示在系统界面之后

    WindowCompat.setDecorFitsSystemWindows(window, false)

  3. 透明状态栏和导航栏

    window.run {
    statusBarColor = Color.TRANSPARENT
    navigationBarColor = Color.TRANSPARENT
    }

    更完美的颜色参照Gesture Navigation: going edge-to-edge (I)

  4. 避免View和系统界面重叠

    如果顶部是AppBarLayout,就增加一个android:fitsSystemWindows="true"来让它自己处理顶部。底部也可以用默认处理,但通常效果不理想。

    如果是普通View:

    ViewCompat.setOnApplyWindowInsetsListener(binding.root) { v, insets ->
    val bottomInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars())
    v.updatePadding(bottom = bottomInsets.bottom)
    val keyboardShown = insets.isVisible(WindowInsetsCompat.Type.ime())
    insets
    }
    • 这里只用systemBars来避免键盘弹出时界面跳动。
    • 这里也是检测键盘是否弹出的地方。
    • 在键盘弹出时,导航栏高度算在软键盘高度的一部分中的。
  5. ViewCompat.setWindowInsetsAnimationCallback(
    binding.root,
    object : WindowInsetsAnimationCompat.Callback(DISPATCH_MODE_STOP) {
    override fun onProgress(
    insets: WindowInsetsCompat,
    runningAnimations: MutableList<WindowInsetsAnimationCompat>
    ): WindowInsetsCompat {
    val bottomInsets = insets.getInsets(
    WindowInsetsCompat.Type.ime() or
    WindowInsetsCompat.Type.systemBars()
    )
    binding.root.updatePadding(bottom = bottomInsets.bottom)
    return insets
    }
    }
    )
    • 如果要显示Snackbar,需要额外测试。Padding可能不是最好的方法,可能需要Margin或额外的Space View。
    • 显示软键盘,似乎Showing the Android Keyboard ReliablyWindowInsetsController处理的情况更多且更好用。关闭的话,WindowInsetsController就可以。

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK