大家都知道,很多Android手机带有重力感应传感器,能够对手机的翻转做出响应。比如应用在屏幕的自动翻转、重力感应游戏等方面。
  只要在androidmanifest.xml中对应的Activity中加入sensor属性即可实现屏幕自动翻转,如:
  Xml代码
  <
  activity android:name=".demo"
  android:label="@string/app_name"
  android:screenOrientation="sensor"
  >
  但是屏幕自动翻转也伴随着一个问题:当窗体切换或者布局切换时,Activity中OnCreate方法会被重复调用。一般OnCreate中会初始化一些数据,重复调用可能会产生意想不到的后果。解决方法如下:
  在androidmanifest.xml中的activit元素加入configChanges这个属性,比如
  Xml代码
  <
  activity android:name="demo"
  android:configChanges="orientation|keyboardHidden"
  android:label="@string/app_name"
  >
  另外,在Activity的Java文件中重载onConfigurationChanged(Configuration newConfig)这个方法,这样就不会在布局切换或窗口切换时重载onCreate等方法。代码如下:

  Java代码
  public void onConfigurationChanged(Configuration newConfig)
  {
  super.onConfigurationChanged(newConfig);
  if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
  {
  //TO-DO
  }
  else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
  {
  //TO-DO
  }
  }

  还有界面设计方面的问题,Android手机大部分是HVGA、WVGA的分辨率,屏幕视觉上比较“狭长”。往往竖着看很合适的布局,当屏幕横向翻转以后显示会变得很别扭。当屏幕由竖直方向改变为横向时,我们可以把界面中的控件由本来的垂直线性布局修改为横向线性布局,这样布局会更合理一些。我们可以自己写一个布局类集成LinearLayout布局,通过覆盖onMeasure方法来实现这种自动布局。当屏幕的宽高发生改变时,系统会调用 onMeasure方法。通过这个方法,我们可以获得改变以后的宽高尺寸,从而来实现屏幕翻转的自动布局,主要代码如下:
  Java代码

  /**
  * 屏幕改变时自动调用
  * @param widthMeasureSpec 改变后的宽度
  * @param heightMeasureSpec 改变后的高度
  */

  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
  {
  /*宽度*/
  int screenWith = View.MeasureSpec.getSize(widthMeasureSpec);
  /*高度*/
  int screenHeight = View.MeasureSpec.getSize(heightMeasureSpec);
  /*竖直布局*/
  if (screenWith < screenHeight)
  {
  this.setOrientation(VERTICAL);
  for (int i = 0; i < getChildCount(); i++)
  {
  View childView = getChildAt(i);
  if (childView instanceof CakyCanvas)
  {
  /*该控件占布局的2/5*/
  LayoutParams params = new LayoutParams(screenWith,
  screenHeight * 2/ 5
  updateViewLayout(childView, params);
  }
  else if (childView instanceof CakyExplainCanvas)
  {
  /*该控件占布局的3/5*/
  LayoutParams params = new LayoutParams(screenWith,
  screenHeight * 3/ 5
updateViewLayout(childView, params);
  }
}
  }
  /*横向布局*/
  else
  {
  this.setOrientation(HORIZONTAL);
  for (int i = 0; i < getChildCount(); i++)
  {
  View childView = getChildAt(i);
  if (childView instanceof CakyCanvas)
  {
  LayoutParams params = new LayoutParams(
  screenWith * 2/ 5
  screenHeight);
  updateViewLayout(childView, params);
  }
  else if (childView instanceof CakyExplainCanvas)
  {
  LayoutParams params = new LayoutParams(
  screenWith * 3/ 5
  screenHeight);
  updateViewLayout(childView, params);
  }
  }
  }
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  }

更多相关文章

  1. 【Android(安卓)应用开发】 Android(安卓)相关代码规范 更新中 .
  2. 《第一行代码Android》学习总结第十三章 Android编程技巧
  3. Android(安卓)如何建立AIDL
  4. android4.2 webkit 中的jni
  5. Android原生集成ReactNative框架
  6. Android(安卓)Studio 1.3 正式发布
  7. android java代码的启动:app_process
  8. Android(安卓)中 java 与 webview 的交互
  9. Android(安卓)内核源代码交叉编译

随机推荐

  1. 2011.06.21(2)——— android invalidate
  2. android framework增加新的系统服务
  3. Android帮助文档.exe(第1,2,3部分)提供下载
  4. Android(安卓)Makefile中是 如何识别 TAR
  5. android framework 图解分享
  6. Android(安卓)Camera源码函数结构
  7. Android(安卓)ImageView 总结
  8. android开发每日汇总【2011-11-26】
  9. 怎么设置Android(安卓)Activity的动画
  10. Android安装和删除(卸载)应用软件程序(ap