gpt4 book ai didi

Android如何获取应用程序区域的大小

转载 作者:行者123 更新时间:2023-11-29 20:53:14 26 4
gpt4 key购买 nike

Nexus6

对于上面的模拟器,在我的onCreate 方法中,如何获取空白区域的大小(高度和宽度)? (不包括 Project1 Screen2 的空间)?

最佳答案

初始化 View 并在创建方法中调用此方法

 view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
@Override
public void onGlobalLayout() {
//now we can retrieve the width and height
int width = view.getWidth();
int height = view.getHeight();
//... //do whatever you want with them //... //this is an important step not to keep receiving callbacks: //we should remove this listener //I use the function to remove it based on the api level!
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN)
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
else
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} });

关于Android如何获取应用程序区域的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28523122/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com