kar*_*rl_ 8 android backwards-compatibility deprecated
那么我该如何编写代码来适应这个呢?我不想在我的代码中留下已弃用的API调用,但我也不想让(稍微)较旧的设备丢失用户.我可以实现某种兼容性设置吗?
相对.码
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int screen_width = size.x;
int screen_height = size.y;
Run Code Online (Sandbox Code Playgroud)
与旧方法:
int screen_width = getWindowManager().getDefaultDisplay().getWidth();
int screen_height = getWindowManager().getDefaultDisplay().getHeight();
Run Code Online (Sandbox Code Playgroud)
Jor*_*sys 12
我有两个函数,发送上下文和gettin高度和宽度(以像素为单位).
public static int getWidth(Context mContext){
int width=0;
WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
if(Build.VERSION.SDK_INT>Build.VERSION_CODES.HONEYCOMB){
Point size = new Point();
display.getSize(size);
width = size.x;
}
else{
width = display.getWidth(); // deprecated
}
return width;
}
Run Code Online (Sandbox Code Playgroud)
和
public static int getHeight(Context mContext){
int height=0;
WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
if(Build.VERSION.SDK_INT>Build.VERSION_CODES.HONEYCOMB){
Point size = new Point();
display.getSize(size);
height = size.y;
}else{
height = display.getHeight(); // deprecated
}
return height;
}
Run Code Online (Sandbox Code Playgroud)
最好的(最好的,我的意思是几乎每次都有效的选项)选项是使用反射。查看 Android向后兼容性 向后兼容性 guidelines (updated with new location of the article on reflection).
虽然 tyczj 的答案将完美工作,只要已弃用的功能仍在 SDK 中,但一旦删除它们,如果您仍想针对最新的 SDK 进行构建,您将无法使用它们或在旧设备上运行您的应用程序。
反射通过在运行时有效地动态检测函数来解决这个问题,这意味着即使您针对 ICS 进行构建,只要 minSdkVersion 正确,您就可以让您的应用程序在带有 Gingerbread 或 Froyo 的设备上运行。
| 归档时间: |
|
| 查看次数: |
10608 次 |
| 最近记录: |