---
title: "Flex 4 舞台尺寸随浏览器窗口大小改变"
author: "xchangei"
date: "2012-04-11T17:56:18Z"
tags: ["Flex"]
categories: ["Web前端"]
slug: "flex-4"
draft: false
lastmod: "2012-04-11T17:56:18Z"
id: 1483553
url: "https://xchangei.iteye.com/blog/1483553"
---

首先创建项目修改启动html文件 （html-template 文件夹里的 index.template.html 文件） \[code="html"] ${title}

To view this page ensure that Adobe Flash Player version ${version\_major}.${version\_minor}.${version\_revision} or greater is installed.

\[/code] 增加的 代码是 （用来获取浏览器的尺寸 和 发送数据到 flex） \[code="html"] var winWidth = 0; var winHeight = 0; function findDimensions() { //获取窗口宽度 if (window.innerWidth) { winWidth = window.innerWidth; } else if ((document.body) && (document.body.clientWidth)) { winWidth = document.body.clientWidth; //获取窗口高度 } if (window.innerHeight) { winHeight = window.innerHeight; } else if ((document.body) && (document.body.clientHeight)){ winHeight = document.body.clientHeight; } //通过深入Document内部对body进行检测，获取窗口大小 if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth) { winHeight = document.documentElement.clientHeight; winWidth = document.documentElement.clientWidth; } if(isReady()){ var str = winWidth+","+winHeight; sendToActionScript(str); } } window.onresize=findDimensions; var jsReady = false; function isReady() { return jsReady; } function pageInit() { jsReady = true; //调用函数，获取数值 findDimensions(); } function thisMovie(movieName) { if (navigator.appName.indexOf("Microsoft") != -1) { return window\[movieName]; } else { return document\[movieName]; } } function sendToActionScript(value) { thisMovie("${application}").sendToActionScript(value); } function sendToJavaScript(value) { findDimensions(); } \[/code] 修改 样式 增加宽度100%设定 html, body { height:100%; width:100%; } 修改body 标签 监听加载完成事件 之后的宽度高度 都改成 100% 就好了 在看 flex 代码 如下 \[code="xml"] \[/code] flex 的就不说了 那几个和js通讯的函数 和 application1\_creationCompleteHandler 方法里 对通讯的启动 好了 可以启动浏览起 拖拽浏览器改变窗口大小 看看 按钮会不会自适应尺寸了 做个tag保留