Rob*_*mes 11 eclipse sdk android backwards-compatibility android-actionbar
我想使用SDK 11中包含的操作栏功能.但是我还希望该应用程序能够在SDK 10(2.3.3)的早期设备上运行.我愿意放弃早期设备的操作栏功能,因为它不是一个重要的功能.我已经完成了关于反射,包装类和其他一些技术的所有阅读.我现在很难知道如何使这项工作.我正在使用Eclipse.
如果我没有将Eclipse中的目标设置为sdk 11或更高版本,那么我对actionBar的引用的任何地方都会产生编译错误.如果我将目标放到sdk 11或更高版本,它会编译,但不会显示它可以在早期设备上运行.我一直android:minSdkVersion=10都在设置.
有人可以给我一些关于如何进行引用的知识,actionBar并让它以之前的sdk级别为目标吗?提前致谢.
twa*_*ton 18
是! 你绝对可以做到这一点.请尝试遵循下面列出的模式.
在您的AndroidManifest.xml文件中声明以下内容(将平台版本替换为您的应用程序所需的任何内容):
<!-- Build Target -->
<uses-sdk android:targetSdkVersion="14" android:minSdkVersion="7" />
Run Code Online (Sandbox Code Playgroud)
通过针对API 11或更高版本的平台版本,您允许Eclipse链接(编译)本机ActionBar类.提供较早的最低平台版本允许您的应用程序在旧版Android上安装(运行).
您的活动代码应该如下所示:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (CompatibilityManager.isHoneycomb()) {
final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(true);
// ...
} else {
// The ActionBar is unavailable!
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
凡CompatibilityManager.java类简单地规定:确定该SDK的当前版本的静态辅助方法:
public class CompatibilityManager {
public static final String KINDLE_FIRE_MODEL = "Kindle Fire";
/**
* Get the current Android API level.
*/
public static int getSdkVersion() {
return android.os.Build.VERSION.SDK_INT;
}
/**
* Determine if the device is running API level 11 or higher.
*/
public static boolean isHoneycomb() {
return getSdkVersion() >= Build.VERSION_CODES.HONEYCOMB;
}
/**
* Determine if the device is running API level 14 or higher.
*/
public static boolean isIceCreamSandwich() {
return getSdkVersion() >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
}
/**
* Determine if the current device is a first generation Kindle Fire.
* @return true if the device model is equal to "Kindle Fire", false if otherwise.
*/
public static boolean isKindleFire() {
return Build.MODEL.equals(KINDLE_FIRE_MODEL);
}
}
Run Code Online (Sandbox Code Playgroud)
您也可以考虑利用ActionBarSherlock库,它提供兼容的ActionBar API,一直到Android 2.x:
该库将在可用时自动使用本机操作栏,或者将自动在您的布局周围包装自定义实现.这使您可以通过2.x轻松地为每个版本的Android开发一个带有操作栏的应用程序.
玩得开心!
| 归档时间: |
|
| 查看次数: |
7312 次 |
| 最近记录: |