`
iaiai
  • 浏览: 2143557 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Android 4.0 ( Ice Cream Sandwich) 即将到来 你准备好了吗

 
阅读更多
日前developer.android.com 的首席技术作家 Scott Main 为 Android Developers Blog 撰写了一篇文章介绍如何把为Android 3.0平板开发的程序移植到即将发布的Ice Cream Sandwich 系统上.

在文中 Scott Main 还提到,Android系统从Ice Cream Sandwich以后都将保持让同一个App运行在各种尺寸的屏幕上,让开发者不再为屏幕尺寸不统一而烦恼!

但是 那些为 Honeycomb 系统而开发的平板程序,是针对大尺寸屏幕设计的, 在Ice Cream Sandwich 尚未发布的时候 这些程序还不会对用户照成什么干扰, 而 在Ice Cream Sandwich发布以后 就不是这么一回事了, 由于Android系统是向后兼容的, 所以等Ice Cream Sandwich系统发布以后,有用户使用该系统的小屏幕手机来使用你为Honeycomb开发的大屏幕程序就会出现问题. 为了避免这种伤害用户的行为,现在你就可以开始修改你的程序了.

对于那些专门为Honeycomb平板而开发的程序,需要做两件事情:

1.阻止该程序安装到小屏幕设备上;
2.修改该程序 使其可以兼容小屏幕设备

只支持Honeycomb平板
如果你的程序只支持大屏幕的平板,则修改程序是比较简单的,只需要在 manifest 文件中添加  <supports-screens> 标签即可:
<manifest ... >
    <supports-screens android:smallScreens="false"
                      android:normalScreens="false"
                      android:largeScreens="false"
                      android:xlargeScreens="true"
                      android:requiresSmallestWidthDp="600" />
    <application ... >
        ...
    </application>
</manifest>

上面的配置文件说明了:
1.程序不支持 “small”, “normal”, and “large”这3种类型的屏幕, 这些属性应用于3.1或3.1之前的系统,通常这些类型的设备都不是平板
2.requiresSmallestWidthDp属性应用于3.1之后的系统,定义了该程序需要的最小屏幕宽度为600dp, 这通常是大于等于7英寸的屏幕, 如果你的程序是为大于等于9英寸的屏幕设计的,则可以设置该值为 720dp.

由于添加了requiresSmallestWidthDp 这个属性,所以你的程序需要使用3.2或者更新的SDK来编译和发布. 在开发程序的时候 为了保险起见,建议先使用minSdkVersion 指定版本的SDK来开发程序,等程序开发好了再加上requiresSmallestWidthDp这个属性,然后使用3.2版本的SDK来发布程序. 这样的话 就避免了你无意中使用了3.2中的API而导致程序在低版本系统上崩溃的情况.

让Honeycomb程序支持小屏幕设备
如果你想让为平板系统开发程序也运行在小屏幕设备上,则推荐修改你的程序让其支持小屏幕,而不是发布多个APK来支持不同的屏幕.
由于Ice Cream Sandwich支持 Honeycomb的所有API,所以对于支持以下2要素的app 在移植这些程序的时候并不是很复杂.
使用Fragments 来设计程序,这样可以在不同的屏幕上重用该设计,在小屏幕上使用单个面板布局;在大屏幕上使用多个面板布局
注意Action Bar 的设计要灵活,这样当屏幕空间不可用的时候,系统可以把Action Bar 转化为菜单显示
下面我们将介绍一种简单适配的开发方法:


上图显示了在小屏幕和大屏幕设备上使用fragment的显示方式.
在上图所示的App中, Activity A 是程序入口,其根据屏幕大小用不同的layout文件来显示一个或者两个fragments. 当在小屏幕设备上 layout文件只包含Fragment A(图中的List View);当在大屏幕设备(平板)上 layout文件包含Fragment A和Fragment B 两个Fragments.
下面是用于小屏幕的layout文件: res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment
              android:id="@+id/list_frag"
              android:layout_width="match_parent"
              android:layout_height="match_parent"/>
</FrameLayout>

下面是用于大屏幕的layout文件:res/layout-large/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/frags">
  <fragment
            android:id="@+id/list_frag"
            android:layout_width="@dimen/titles_size"
            android:layout_height="match_parent"/>
  <fragment
            android:id="@+id/details_frag"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
</LinearLayout>


当用户选择列表中的一个选项的时候, 程序如何响应依赖于Fragment B是否显示在当前layout中. 如果Fragment B显示则 Activity A只要通知Fragment B更新自己就可以了; 如果Fragment B没有显示 则Activity A启动 Activity B(Activity B用来显示Fragment B);
1.要在你的程序中使用这种设计模式,需要每个Fragment都相对独立. 下面有两个建议:
不要直接从一个Fragment中操作另外一个Fragment
2.把和Fragment中包含的内容有关的代码放到该Fragment中, 而不是把这些代码放到该Fragment的Activity中.

为了避免直接从一个Fragment中调用另外一个Fragment, 可以在每个Fragment中都声明一个回调接口,可以通过该接口把事件通知给该Fragment所在的Activity(该Activity实现回调接口),当Activity收到事件的时候 就根据Fragment的配置来触发不同的操作.
例如 在上图所示的  Activity A 中 ,当用户选择一个ListItem的时候的动作如下:
/** This is a callback that the list fragment (Fragment A) calls
    when a list item is selected */
public void onItemSelected(int position) {
  DisplayFragment fragB = (DisplayFragment) getFragmentManager()
                              .findFragmentById(R.id.display_frag);
  if (fragB == null) {
      // DisplayFragment (Fragment B) is not in the layout,
      // start DisplayActivity (Activity B)
      // and pass it the info about the selected item
      Intent intent = new Intent(this, DisplayActivity.class);
      intent.putExtra("position", position);
      startActivity(intent);
  } else {
      // DisplayFragment (Fragment B) is in the layout, tell it to update
      fragB.updateContent(position);
  }
}


当DisplayActivity (Activity B) 启动的时候, 通过读取Intent中的数据来显示对应的内容.
如果 Fragment B 需要告诉 Fragment A 一个处理结果, 那么实现的逻辑是一样
如果 Fragment B 需要告诉 Fragment A 一个处理结果, 那么实现的逻辑是一样的, Fragment B 和 Activity B 通过回调接口来传递事件. 具体就是: Activity B 实现一个由 Fragment B 定义的回调接口.当 Activity B 收到事件,就设置结果然后关闭(finish)自己. Activity A  收到事件 然后通知 Fragment A.
具体示例可以下载这个项目( Honeycomb Gallery sample)来看看.
对于 Action Bar的设计 可以参考 前面的一篇文章:  使用XML文件来配置Android程序的菜单
最后 就是不用忘记了测试下你的App, 看看其行为是否一致.
当然了 在Ice Cream Sandwich SDK发布之前 你是没法测试小屏幕的, 但是这里偷偷的告诉你一个小窍门, 你可以在Honeycomb中通过屏幕方向来分别代表不同的屏幕尺寸. 例如 竖屏的时候 使用小屏幕的布局文件; 横屏的时候使用大屏幕的布局文件, 这样只要旋转下屏幕就可以测试~\(≧▽≦)/~啦啦啦.
分享到:
评论
1 楼 icess 2012-03-03  
转载请注明出处好不好啊 老兄
http://yunfeng.sinaapp.com/?p=186

相关推荐

Global site tag (gtag.js) - Google Analytics