错误获取在Android窗口大小:"该方法getWindowManager是未定义" [英] Error getting window size on android: "The method getWindowManager is undefined"

查看:1587
本文介绍了错误获取在Android窗口大小:"该方法getWindowManager是未定义"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要得到窗口的高度和重量。
我有这个在我的code:

  DisplayMetrics指标=新DisplayMetrics();
。getWindowManager()getDefaultDisplay()getMetrics(度量)。
宽度1 = metrics.widthPixels;
height1 = metrics.heightPixels;
 

但我有一个错误:

  

该方法getWindowManager未定义

我应该导入这个? 或者我可以用不同的方式熨平板的大小? 我已经进口

 进口android.view.Display;
进口android.view.Window;
进口android.view.WindowManager;
进口android.content.Context;
进口android.util.DisplayMetrics;
 

解决方案

你在哪里使用这个code?

的误差 getWindowManager 表示,无法找到方法,因为你的类的根没有了。

试试这个:

 ((活动)的getContext())getWindowManager()getDefaultDisplay()getMetrics(指标)。;
 

 ((窗口管理器)的getContext()。getSystemService(Context.WINDOW_SERVICE))
  .getDefaultDisplay()getMetrics(度量)。
 

如果这不起作用,请粘贴您的活动

I need to get the window height and the weight.
I have this in my code:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
width1 = metrics.widthPixels; 
height1 = metrics.heightPixels;

but I have an error:

The method getWindowManager is undefined

what i should import for this? or i can get the sizes of the screed in different ways? i have imported

import android.view.Display;
import android.view.Window;
import android.view.WindowManager;
import android.content.Context;
import android.util.DisplayMetrics;

解决方案

Where are you using this code?

The error of the getWindowManager says that cannot find the method because the root of your class doesn't have it.

Try this:

((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(metrics);

or

((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE))
  .getDefaultDisplay().getMetrics(metrics);

If this doesn't work, please paste your activity

这篇关于错误获取在Android窗口大小:"该方法getWindowManager是未定义"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆