内容视图的宽度和高度 [英] content view width and height

查看:228
本文介绍了内容视图的宽度和高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的步骤来了解内容查看的宽度和高度,我的显示屏幕上

I am using below steps to know the width and height of contentview on my display screen

ContentViewWidth = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getWidth();
ContentViewHeight = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getHeight();

但这种方法并不里面的onCreate或onResume工作,如果我使用的内部onWindowFocusChanged这些步骤,我得到我的手机上强制关闭错误

but this method doesn't work inside onCreate or onResume and if I am using these steps inside onWindowFocusChanged I get force close error on my phone

请帮助我,我怎样才能让我的显示画面的内容视图。内容的看法是不包括状态栏和标题栏尺寸

Please help me how can I get the content view of my display screen. Content view is excluding statusbar and titlebar dimensions

推荐答案

我用下面的方法 - 发布可运行从的onCreate()将被执行时的观点已创建:

I use the following technique - post a runnable from onCreate() that will be executed when the view has been created:

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

这code将运行在主UI线程上,在的onCreate()已完成。

This code will run on the main UI thread, after onCreate() has finished.

如果你想获取内容视图本身的高度,你需要从高度减去填充 - 这是由于Android 2.X勾画出不同的意见到4.x

If you are trying to get the height of the content view itself, you need to subtract the padding from the height - this is because Android 2.x lays out the views differently to 4.x.

contentHeight = contentView.getHeight() - contentView.getTopPadding();

这篇关于内容视图的宽度和高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆