You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用Share Intent时Toolbar上滑,如何修复?

修复Share Intent调用时Toolbar上滑的问题

以下是几种可行的解决方案:

方案1:调整Activity的Window属性

在AndroidManifest.xml中对应Activity的标签内,添加或修改android:windowSoftInputMode属性,避免布局因系统界面触发重排:

<activity
    android:name=".YourActivity"
    android:windowSoftInputMode="adjustPan">
    <!-- 其他配置 -->
</activity>

adjustPan会让界面整体上移以规避遮挡,同时能阻止分享界面引发的布局偏移;若无需适配软键盘,也可使用adjustNothing,完全禁止布局调整。

方案2:固定Toolbar的布局约束

确保Toolbar在布局文件中绑定到父布局的固定顶部位置:

  • 若使用ConstraintLayout:
<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"/>
  • 若使用LinearLayout,将Toolbar设为首个子项,并保持垂直布局的顶部对齐:
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"/>

    <!-- 其他视图 -->
</LinearLayout>

方案3:禁用Activity过渡动画

系统分享界面的过渡动画可能导致Toolbar偏移,启动Intent时禁用动画即可:

val shareIntent = Intent(Intent.ACTION_SEND).apply {
    type = "text/plain"
    putExtra(Intent.EXTRA_TEXT, "待分享内容")
}
startActivity(Intent.createChooser(shareIntent, "选择分享渠道"))
// 禁用进入/退出动画
overridePendingTransition(0, 0)

方案4:适配沉浸式状态栏(若启用)

如果开启了沉浸式状态栏,系统界面切换可能导致Toolbar位置计算错误,可在onResume中重新设置状态栏padding:

override fun onResume() {
    super.onResume()
    toolbar.setPadding(0, getStatusBarHeight(), 0, 0)
}

private fun getStatusBarHeight(): Int {
    var height = 0
    val resId = resources.getIdentifier("status_bar_height", "dimen", "android")
    if (resId > 0) {
        height = resources.getDimensionPixelSize(resId)
    }
    return height
}

内容的提问来源于stack exchange,提问作者Viacheslav Kolisnichenko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 05:27:18