错误获取在Android窗口大小:"该方法getWindowManager是未定义"大小、窗口、错误、未定义

2023-09-05 00:29:23 作者:岛是海心上的疤

我需要得到窗口的高度和重量。 我有这个在我的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(指标)。;
 
安卓BS模拟器怎么改变窗口大小

 ((窗口管理器)的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