内容视图的宽度和高度视图、宽度、高度、内容

由网友(仙女味的软糖)分享简介:我使用下面的步骤来了解内容查看的宽度和高度,我的显示屏幕上I am using below steps to know the width and height of contentview on my display screenContentViewWidth = getWindow().findViewByI...

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

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();
阅读全文

相关推荐

最新文章