java Android:我应该怎么做而不是使用已弃用的函数(getwidth())?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15067482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android : What should I do instead of using a deprecated function(getwidth() )?
提问by Navid777
I want to use the function activity.getWindowManager().getDefaultDisplay().getwidth() but there is a warning that says this function is deprecated
我想使用函数 activity.getWindowManager().getDefaultDisplay().getwidth() 但有一个警告说这个函数已被弃用
What should I do ? Should I use this function anyway? or there is some other functions that do the same ?
我该怎么办 ?我应该使用这个功能吗?或者还有其他一些功能可以做同样的事情?
回答by Micha? Z.
Deprecated means that it shouldn't be used, but it's still there for compability reasons.
弃用意味着不应使用它,但出于兼容性原因,它仍然存在。
You should use instead:
你应该使用:
Point size = new Point();
activity.getWindowManager().getDefaultDisplay().getSize(size);
int width = size.x;
int height = size.y;
回答by Maroun
A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists. Compilers warn when a deprecated program element is used or overridden in non-deprecated code.
带有@Deprecated 注释的程序元素是不鼓励程序员使用的,通常是因为它很危险,或者因为存在更好的替代方案。当在非弃用代码中使用或覆盖已弃用的程序元素时,编译器会发出警告。
See thisand thisand thisand thisand thisand so on............
回答by Vlad
From the Display
API reference:
从Display
API 参考:
int getWidth() This method was deprecated in API level 13. Use getSize(Point) instead.
int getWidth() This method was deprecated in API level 13. Use getSize(Point) instead.
Which means you'll instantiate a Point
, pass it to getSize()
and retrieve the x
from it.
这意味着您将实例化 a Point
,将其传递给getSize()
并x
从中检索。
回答by Fahad Ishaque
Deprecated functions are those function of which new better alternates have been introduced and in future they might not be supported in new API's. But feel free to use them as it takes a lot of time for them to get expired.
不推荐使用的函数是那些引入了新的更好的替代函数的函数,将来新的 API 可能不支持它们。但是请随意使用它们,因为它们需要很长时间才能过期。
回答by Piotr
Put your cursor on method's name and press F2
to get informations about latest API. (assuming you're using Eclipse)
将光标放在方法名称上,然后按F2
以获取有关最新 API 的信息。(假设您使用的是 Eclipse)
回答by Hasan A Yousef
Try:
尝试:
WindowManager windowmanager = (WindowManager) this.getContext()
.getSystemService(Context.WINDOW_SERVICE);
with:
和:
Display display = windowmanager.getDefaultDisplay();
Point size = new Point();
try {
display.getRealSize(size);
} catch (NoSuchMethodError err) {
display.getSize(size);
}
int width = size.x;
int height = size.y;
or with:
或与:
DisplayMetrics displayMetrics = new DisplayMetrics();
windowmanager.getDefaultDisplay().getMetrics(displayMetrics);
int deviceWidth = displayMetrics.widthPixels;
int deviceHeight = displayMetrics.heightPixels;
回答by rdrobinson3
The correct thing to do is to check the SDK version. Depending on that you can use the deprecated function or do it using Point. See the following: Is it safe to use .getWidth on Display even though its deprecated.
正确的做法是检查SDK版本。根据这一点,您可以使用已弃用的函数或使用 Point. 请参阅以下内容:在 Display 上使用 .getWidth 是否安全,即使它已弃用。