24

I'm looking for a good way to measure the dimensions of the actual content area for an activity in Android.

Getting display always works. Simply go like this:

Display display = getWindowManager().getDefaultDisplay();

And you can get the pixel count for the entire screen. Of course this does not take into consideration the ActionBar, status bar, or any other views which will reduce the available size of the activity itself.

Once the activity is running, you can do this:

View content = getWindow().findViewById(Window.ID_ANDROID_CONTENT);

To get the activity content only. But doing this in onCreate() will result in a view with width and height of 0, 0.

Is there a way to get these dimensions during onCreate? I imagine there ought to be a way to get the measurements of any status bars and just subtract that from the total display size, but I'm unable to find a way to do that. I think this would be the only way, because the content window method will always return a view with no width/height before it is drawn.

Thanks!

4

6 回答 6

33

您可以为此使用布局或预绘制侦听器,具体取决于您的目标。例如,在 onCreate() 中:

final View content = findViewById(android.R.id.content);
content.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        //Remove it here unless you want to get this callback for EVERY
        //layout pass, which can get you into infinite loops if you ever
        //modify the layout from within this method.
        content.getViewTreeObserver().removeGlobalOnLayoutListener(this);

        //Now you can get the width and height from content
    }
});

从 API 16 开始的更新已被弃用。removeGlobalOnLayoutListener

改成: content.getViewTreeObserver().removeOnGlobalLayoutListener(this)

于 2013-09-18T03:17:44.163 回答
19

(从我对相关问题的回答中复制)

我使用以下技术 - 发布一个可运行的onCreate(),将在创建视图时执行:

    contentView = findViewById(android.R.id.content);
    contentView.post(new Runnable()
    {
        public void run()
        {
            contentHeight = contentView.getHeight();
        }
    });

完成后,此代码将在主 UI 线程上运行onCreate()

于 2014-01-29T08:50:54.350 回答
3

答案post不正确,因为可能不会重新计算大小。
另一个重要的事情是视图及其所有祖先必须是可见的。为此,我使用了一个属性View.isShown

这是我的 kotlin 函数,可以放在 utils 中的某个位置:

fun View.onInitialized(onInit: () -> Unit) {
    viewTreeObserver.addOnGlobalLayoutListener(object : OnGlobalLayoutListener {
        override fun onGlobalLayout() {
            if (isShown) {
                viewTreeObserver.removeOnGlobalLayoutListener(this)
                onInit()
            }
        }
    })
}

用法是:

myView.onInitialized {
    Log.d(TAG, "width is: " + myView.width)
}
于 2020-03-26T20:21:44.310 回答
2

这似乎是一个重复的问题。在这个 SO 问题中有一个优雅的答案:

View 的 getWidth() 和 getHeight() 返回 0

哪个(无耻地复制)是覆盖 onWindowFocusChanged(),它似乎在 onCreate() 之后触发,并且在哪里呈现大小:

@Override
 public void onWindowFocusChanged(boolean hasFocus) {
  super.onWindowFocusChanged(hasFocus);
  //Here you can get the size!
 }
于 2015-08-06T14:10:23.270 回答
1

如果您想将Bitmap加载到ImageViewwithin 中OnCreate(),可以使用以下示例来执行此操作:

public static void setImageLater(@NonNull final ImageView imageView,final Bitmap bitmap){
    final ViewTreeObserver observer = imageView.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    imageView.setImageBitmap(bitmap);
                }
            });
}
于 2018-06-26T20:47:35.443 回答
-2

你可以做任何需要尺寸的工作onResume(使用标志,比如alreadyDone每次Activity进入前台时不要重复它)。在onCreate视图中不显示,因此大小为零是正常的。

于 2013-09-18T02:51:35.877 回答