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

如何在Flutter中为Galaxy Z Fold设置16:9屏幕宽高比?

在Galaxy Z Fold设备上实现16:9屏幕比例的Flutter方案

以下是几种可行的实现方式,解决你设置android:resizeableActivity="false"无效的问题:

方法一:Flutter层强制布局比例

通过Flutter布局组件直接约束整个应用的宽高比,不受系统配置限制:

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Builder(
          builder: (context) {
            // 获取屏幕尺寸
            final screenSize = MediaQuery.of(context).size;
            // 计算16:9的目标尺寸(以短边为基准,避免超出屏幕)
            double targetWidth, targetHeight;
            if (screenSize.width / screenSize.height > 16/9) {
              targetHeight = screenSize.height;
              targetWidth = targetHeight * 16/9;
            } else {
              targetWidth = screenSize.width;
              targetHeight = targetWidth * 9/16;
            }

            return Center(
              child: ConstrainedBox(
                constraints: BoxConstraints(
                  maxWidth: targetWidth,
                  maxHeight: targetHeight,
                ),
                child: Container(
                  width: double.infinity,
                  height: double.infinity,
                  color: Colors.white,
                  // 这里放置你的应用内容
                  child: const Text("16:9布局内容"),
                ),
              ),
            );
          },
        ),
      ),
    );
  }
}

方法二:AndroidManifest配置优化

仅设置resizeableActivity="false"不够,需要同时指定宽高比限制,修改android/app/src/main/AndroidManifest.xml:

<activity
    android:name=".MainActivity"
    android:exported="true"
    android:launchMode="singleTop"
    android:theme="@style/LaunchTheme"
    android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
    android:hardwareAccelerated="true"
    android:windowSoftInputMode="adjustResize"
    <!-- 添加以下宽高比限制 -->
    android:resizeableActivity="false"
    android:maxAspectRatio="1.7777778"
    android:minAspectRatio="1.7777778">

1.7777778是16除以9的近似值,系统会强制Activity保持16:9比例,超出部分显示黑边,和Instagram效果一致。

方法三:平台通道原生适配(复杂场景)

如果前两种方法不满足需求,可通过平台通道调用Android原生API手动设置窗口尺寸:

  1. Flutter端方法封装:
import 'package:flutter/services.dart';

class ScreenRatioManager {
  static const MethodChannel _channel = MethodChannel('screen_ratio');

  static Future<void> set16To9Ratio() async {
    try {
      await _channel.invokeMethod('set16To9Ratio');
    } on PlatformException catch (e) {
      print("设置失败: ${e.message}");
    }
  }
}
  1. Android原生MainActivity实现:
import android.os.Bundle
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel

class MainActivity : FlutterActivity() {
    private val CHANNEL = "screen_ratio"

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
            if (call.method == "set16To9Ratio") {
                val display = windowManager.defaultDisplay
                val size = android.graphics.Point()
                display.getRealSize(size)
                val width = size.x
                val height = size.y

                val targetWidth: Int
                val targetHeight: Int
                if (width / height.toFloat() > 16/9) {
                    targetHeight = height
                    targetWidth = (targetHeight * 16/9).toInt()
                } else {
                    targetWidth = width
                    targetHeight = (targetWidth * 9/16).toInt()
                }

                window.attributes.apply {
                    this.width = targetWidth
                    this.height = targetHeight
                }
                window.attributes = window.attributes
                result.success(null)
            } else {
                result.notImplemented()
            }
        }
    }
}

在Flutter初始化时调用ScreenRatioManager.set16To9Ratio()即可生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 21:20:43