• 9、创建向后兼容的用


     抽象UI接口

    对于很多UI接口,不同版本的Android会提供不同的接口。例如, Tab,在Level = 11(Android3.0 Honeycomb)中使用ActionBar,而 在更低版本的Android中没有ActionBar,但可以用Tab控件代替。

    对于这些情况,如果要开发适应于所有版本的Android应用,就需 要对这些UI接口进行抽象。通常使用接口或抽象类。并利用多态满足不 同Android版本的需求。 

     创建一个Tag抽象类

     抽象ActionBar.Tab 

     1 public abstract class CompatTab {
     2     ...
     3     public abstract CompatTab setText(int resId); 
     4     public abstract CompatTab setIcon(int resId); 
     5     public abstract CompatTab setTabListener(
     6     CompatTabListener callback);
     7     public abstract CompatTab setFragment(Fragment fragment);
     8     public abstract CharSequence getText();
     9     public abstract Drawable getIcon();
    10     public abstract CompatTabListener getCallback(); 
    11     public abstract Fragment getFragment();
    12 ...
    13 }

     抽象TabHelper 

    1 public abstract class TabHelper {
    2       ...
    3      public CompatTab newTab(String tag) {
    4      // This method is implemented in a later lesson.
    5      }
    6      public abstract void addTab(CompatTab tab);
    7      ... 
    8 }

     新的API实现CompatTab 

     1 public class CompatTabHoneycomb extends CompatTab {       
     2         ActionBar.Tab mTab;
     3        ...
     4        protected CompatTabHoneycomb(FragmentActivity activity, String tag) { 
     5        ...
     6        mTab = activity.getActionBar().newTab(); }
     7        public CompatTab setText(int resId) { mTab.setText(resId);
     8        return this;
     9        }
    10 }

     新的API实现TabHelper 

     1 public class TabHelperHoneycomb extends TabHelper { 
     2       ActionBar mActionBar;
     3       ... ...
     4       protected void setUp() {
     5           if (mActionBar == null) {
     6                mActionBar = mActivity.getActionBar();         
     7                 mActionBar.setNavigationMode(
     8                    ActionBar.NAVIGATION_MODE_TABS); 
     9           }
    10       }
    11        ... ... 
    12 }

     用较早版本API实现Tabs 

    通常很难完全将新版本UI移植到老版本上,而且老版本的用户可能对 新版本的UI风格不习惯,所以应尽量使用老版本与新版本对应的UI。例如, 可以用TabWidget和TabHost来实现CompatTab和TabHelper。 

     检测当前的Android版本 

     1 public abstract class TabHelper { 
     2     ...
     3    public static TabHelper createInstance(FragmentActivity activity) {
     4         if (Build.VERSION.SDK_INT >=  
     5                      Build.VERSION_CODES.HONEYCOMB) {
     6              return new TabHelperHoneycomb(activity); 
     7         } else {
     8              return new TabHelperEclair(activity); 
     9         }
    10     }
    11     public CompatTab newTab(String tag) {
    12           if (Build.VERSION.SDK_INT >= 
    13                   Build.VERSION_CODES.HONEYCOMB) { 
    14              return new CompatTabHoneycomb(mActivity, tag);
    15           } else {
    16                return new CompatTabEclair(mActivity, tag);
    17           }  
    18     }
    19     ...
  • 相关阅读:
    call/cc 总结 | Scheme
    用call/cc合成所有的控制流结构
    词法作用域 vs 动态作用域
    数论部分第二节:埃拉托斯特尼筛法
    1022: [SHOI2008]小约翰的游戏John【Nim博弈,新生必做的水题】
    C++面向对象作业1
    数论部分第一节:素数与素性测试【详解】
    基数排序与桶排序,计数排序【详解】
    计蒜客:百度的科学计算器(简单)【python神解】
    优质免费在线学习网站【自用】
  • 原文地址:https://www.cnblogs.com/androidsj/p/3929109.html
Copyright © 2020-2023  润新知