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

Android中使用startActivity能否指定分屏区域显示目标Activity?

可以指定设置Activity显示在屏幕下半部分,但仅支持Android 7.0+

要实现这个需求,需要利用Android 7.0(API 24)引入的分屏多窗口API,具体操作如下:

  • 先检查系统版本:分屏功能从API 24开始支持,必须先判断当前设备系统版本是否达标
  • 设置Intent标志:给启动设置Activity的Intent添加FLAG_ACTIVITY_LAUNCH_ADJACENT和FLAG_ACTIVITY_NEW_TASK两个标志——前者告诉系统要在相邻分屏区域启动新Activity,后者因为设置是系统应用,需要放在新任务栈中启动
  • 指定启动区域:通过ActivityOptions的setLaunchBounds方法,传入代表屏幕下半部分的Rect坐标,明确新Activity的显示位置

Kotlin代码示例

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    val settingsIntent = Intent(Settings.ACTION_SETTINGS)
    settingsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT)
    
    val screenMetrics = resources.displayMetrics
    val lowerScreenBounds = Rect(
        0, 
        screenMetrics.heightPixels / 2, 
        screenMetrics.widthPixels, 
        screenMetrics.heightPixels
    )
    
    val launchOptions = ActivityOptions.makeBasic().apply {
        setLaunchBounds(lowerScreenBounds)
    }
    
    startActivity(settingsIntent, launchOptions.toBundle())
}

Java代码示例

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    Intent settingsIntent = new Intent(Settings.ACTION_SETTINGS);
    settingsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT);
    
    DisplayMetrics screenMetrics = getResources().getDisplayMetrics();
    Rect lowerScreenBounds = new Rect(
        0, 
        screenMetrics.heightPixels / 2, 
        screenMetrics.widthPixels, 
        screenMetrics.heightPixels
    );
    
    ActivityOptions launchOptions = ActivityOptions.makeBasic();
    launchOptions.setLaunchBounds(lowerScreenBounds);
    
    startActivity(settingsIntent, launchOptions.toBundle());
}

注意事项

  • 如果当前屏幕已经处于双分屏状态,系统可能会根据实际情况调整布局,不一定完全遵循你指定的区域
  • 部分定制化ROM可能对分屏启动逻辑有修改,建议在目标设备上测试验证
  • 低于Android 7.0的系统不支持分屏,无法实现此功能

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 23:02:15