我创建了一些自定义元素,并希望以编程方式将它们放置在右上角(距离上边缘n个像素,距离右边缘m个像素)。因此,我需要获得屏幕宽度和屏幕高度,然后设置位置:
int px = screenWidth - m;
int py = screenHeight - n;
如何在主活动中获取screenWidth和screenHeight?
我创建了一些自定义元素,并希望以编程方式将它们放置在右上角(距离上边缘n个像素,距离右边缘m个像素)。因此,我需要获得屏幕宽度和屏幕高度,然后设置位置:
int px = screenWidth - m;
int py = screenHeight - n;
如何在主活动中获取screenWidth和screenHeight?
当前回答
如果您想要以像素为单位的显示尺寸,可以使用getSize:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
如果您不在“活动”中,则可以通过WINDOW_SERVICE获取默认显示:
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
如果您在一个片段中,并且想要实现这一点,只需使用Activity.WindowManager(在Xamarin.Android中)或getActivity().getWindowManager()(在java中)。
在引入getSize之前(在API级别13中),您可以使用现在已弃用的getWidth和getHeight方法:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
然而,对于用例,您正在描述布局中的边距/填充似乎更合适。
另一种方法是:DisplayMetrics
一种结构,描述有关显示器的一般信息,如大小、密度和字体比例。要访问DisplayMetrics成员,请初始化如下对象:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
我们可以使用widthPixels获取以下信息:
“显示的绝对宽度(以像素为单位)。”
例子:
Log.d("ApplicationTagName", "Display width in px is " + metrics.widthPixels);
API等级30更新
final WindowMetrics metrics = windowManager.getCurrentWindowMetrics();
// Gets all excluding insets
final WindowInsets windowInsets = metrics.getWindowInsets();
Insets insets = windowInsets.getInsetsIgnoringVisibility(WindowInsets.Type.navigationBars()
| WindowInsets.Type.displayCutout());
int insetsWidth = insets.right + insets.left;
int insetsHeight = insets.top + insets.bottom;
// Legacy size that Display#getSize reports
final Rect bounds = metrics.getBounds();
final Size legacySize = new Size(bounds.width() - insetsWidth,
bounds.height() - insetsHeight);
其他回答
最简单的方法:
int screenHeight = getResources().getDisplayMetrics().heightPixels;
int screenWidth = getResources().getDisplayMetrics().widthPixels;
此函数以英寸为单位返回近似屏幕大小。
public double getScreenSize()
{
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;
int dens=dm.densityDpi;
double wi=(double)width/(double)dens;
double hi=(double)height/(double)dens;
double x = Math.pow(wi,2);
double y = Math.pow(hi,2);
double screenInches = Math.sqrt(x+y);
return screenInches;
}
创建一个类和方法,如下所示:
public MyPoint getScreenDimensionsAsPixel(Context context){
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point point = new Point();
display.getSize(point);
return new MyPoint(point.x, point.y);
}
public class MyPoint{
private int width;
private int height;
public MyPoint(int width, int height) {
this.width = width;
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
然后在代码中使用它们:
MyPoint myPoint = getScreenDimensionsAsPixel(MainActivity.this);
Toast.makeText(MainActivity.this, "width: " + String.valueOf(myPoint.getWidth()) + "|" +
"height: " + String.valueOf(myPoint.getHeight()), Toast.LENGTH_LONG).show();
在“活动”中使用以下代码。
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int wwidth = metrics.widthPixels;
创建Kotlin扩展函数以获取屏幕宽度和高度-
fun Context?.screenWidthInPx(): Int {
if (this == null) return 0
val dm = DisplayMetrics()
val wm = this.getSystemService(Context.WINDOW_SERVICE) as WindowManager
wm.defaultDisplay.getMetrics(dm)
return dm.widthPixels
}
//comment
fun Context?.screenHeightInPx(): Int {
if (this == null) return 0
val dm = DisplayMetrics()
val wm = this.getSystemService(Context.WINDOW_SERVICE) as WindowManager
wm.defaultDisplay.getMetrics(dm)
return dm.heightPixels
}