我创建了一些自定义元素,并希望以编程方式将它们放置在右上角(距离上边缘n个像素,距离右边缘m个像素)。因此,我需要获得屏幕宽度和屏幕高度,然后设置位置:
int px = screenWidth - m;
int py = screenHeight - n;
如何在主活动中获取screenWidth和screenHeight?
我创建了一些自定义元素,并希望以编程方式将它们放置在右上角(距离上边缘n个像素,距离右边缘m个像素)。因此,我需要获得屏幕宽度和屏幕高度,然后设置位置:
int px = screenWidth - m;
int py = screenHeight - n;
如何在主活动中获取screenWidth和screenHeight?
当前回答
以下是低于/高于API 30代码的Kotlin扩展函数:
fun Activity.getScreenWidth(): Int {
return if (Build.VERSION.SDK_INT < 30) {
val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(displayMetrics)
displayMetrics.widthPixels
} else {
val metrics = windowManager.currentWindowMetrics
val insets = metrics.windowInsets
.getInsetsIgnoringVisibility(WindowInsets.Type.systemBars())
metrics.bounds.width() - insets.left - insets.right
}
}
fun Activity.getScreenHeight(): Int {
return if (Build.VERSION.SDK_INT < 30) {
val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(displayMetrics)
displayMetrics.heightPixels
} else {
val metrics = windowManager.currentWindowMetrics
val insets = metrics.windowInsets
.getInsetsIgnoringVisibility(WindowInsets.Type.systemBars())
metrics.bounds.height() - insets.top - insets.bottom
}
}
对应的Java助手方法:
public int getScreenWidth(Activity activity) {
if (Build.VERSION.SDK_INT < 30) {
DisplayMetrics displayMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics.widthPixels;
} else {
WindowMetrics metrics = activity.getWindowManager().getCurrentWindowMetrics();
Insets insets = metrics.getWindowInsets()
.getInsetsIgnoringVisibility(WindowInsets.Type.systemBars());
return metrics.getBounds().width() - insets.left - insets.right;
}
}
public int getScreenHeight(Activity activity) {
if (Build.VERSION.SDK_INT < 30) {
DisplayMetrics displayMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics.heightPixels;
} else {
WindowMetrics metrics = activity.getWindowManager().getCurrentWindowMetrics();
Insets insets = metrics.getWindowInsets()
.getInsetsIgnoringVisibility(WindowInsets.Type.systemBars());
return metrics.getBounds().height() - insets.bottom - insets.top;
}
}
其他回答
如果您不想增加WindowManagers、Points或Displays的开销,可以获取XML中最顶层View项的高度和宽度属性,前提是其高度和宽度设置为match_parent。(只要您的布局占据整个屏幕,这是正确的。)
例如,如果您的XML以以下内容开头:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/entireLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
然后findViewById(R.id.entireLayout).getWidth()将返回屏幕的宽度,findViewById(R.id.entireLayout).getHeight()将显示屏幕的高度。
此函数以英寸为单位返回近似屏幕大小。
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 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>12){
Point size = new Point();
display.getSize(size);
width = size.x;
}
else{
width = display.getWidth(); // Deprecated
}
return width;
}
and
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>12){
Point size = new Point();
display.getSize(size);
height = size.y;
}
else{
height = display.getHeight(); // Deprecated
}
return height;
}
它可能无法回答您的问题,但如果你需要一个视图的维度,但你的代码在尚未布局时(例如在onCreate()中)正在执行,你可以使用View.getViewTreeObserver().addOnGlobalLayoutListener()设置一个ViewTreeObserver.OnGlobalLayout侦听器,并将需要该视图的相关代码放入尺寸。布局布局完成后,将调用侦听器的回调。
在Kotlin要简单得多。
val displayMetrics = Resources.getSystem().displayMetrics
displayMetrics.heightPixels
displayMetrics.widthPixels