安卓:获取屏幕尺寸为根布局只 [英] Android: Get Screen size for the root Layout only

查看:110
本文介绍了安卓:获取屏幕尺寸为根布局只的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请给我正确地看过来:

我想获得的可用空间,活动/布局中的onCreate()方法的高度/宽度来计算,可以给孩子布置的高度。我可以用得到的屏幕尺寸:

I want to get the height/width of the space available to the Activity/Layout in onCreate() method to calculate the height that can be given to child layouts. I can get the screen size using :

      root = (LinearLayout) findViewById(R.id.mainroot); // Main layout of LinearLayout  

       android.view.Display display = getWindowManager().getDefaultDisplay();
    int height = Display.getHeight(); // I know this is deprecated have hence used 
      int width = Display.getWidth(); // DisplayMetrics
    int childWidth, childHeight;

    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    //int density = metrics.densityDpi;
    height = metrics.heightPixels;  //480
    width = metrics.widthPixels;    //320

这两种方法/办法给我相同的高度和全屏幕的宽度,即大小。我所寻找的是让被avaialbe扣除的应用程序标题,状态栏等布局后的实际高度。

This both the methods/ways gives me same height and width i.e. size of full screen. What I am looking for is to get actual height that is avaialbe for the layout after deduction of Application Title, Status Bar, etc.

任何想法如何得到这个。或获得冠军,等的大小 - 什么都应该在这里数了。在模拟器我看到2条之上 - 1必须应用titile什么的是另外一个。我可以让他们所有的高度和距离屏幕高度扣除。

Any idea how to get this. OR to get the sizes of titles, etc - what all should be counted over here. On emulator I see 2 bars on top - 1 must be application titile what an be the other one. I can get heights of them all and deduct from screen height.

还有一点:在这种情况下,我会programamtically设置高度,以便根据这将是像素(因为我可以用像素自动调用setHeight只有我猜)将在与目前存在的屏幕尺寸密度的因素影响。什么可以是一个方法来计算高度(可以说50%)的子布局,这将是相同的任何密度Ø左右。

ONE more point : In this case I will be setting the height programamtically so it will be pixel based (as I can setheight in pixels only I guess) will that affect in density factor with differnet screen sizes. What can be a way to calculate height (lets say 50%) for child layout that will be same for any density o so.

推荐答案

解决方法:

在我的onCreate(),我增加了以下几行:

In my onCreate(), I added the following lines :

    setContentView(R.layout.mainpage);

    root = (LinearLayout) findViewById(R.id.mainroot);    
    root.post(new Runnable() {
        public void run() {
            Rect rect = new Rect();
            Window win = getWindow();
            win.getDecorView().getWindowVisibleDisplayFrame(rect);
            int statusHeight = rect.top;
            int contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop();
            titleHeight = contentViewTop - statusHeight;
            Log.i(Utility.TAG, "titleHeight = " + titleHeight + " statusHeight = " + statusHeight + " contentViewTop = " + contentViewTop);

            // CALCULATE THE SIZE OF INNER LAYOUTS
            calculateChildSize();
        }
    });

通过上面的code,我得到titleBar的与放大器的价值观;状态栏。从扣除它 metrics.heightPixels; 我得到所需要的画面高度

With the above code, I get the values of titleBar & statusBar. On deducting it from metrics.heightPixels; I get the required height of the screen.

好的一点是这个code适用于所有密度的。

Good Point is this code works for all density's.

希望这有助于其他人了。

Hope this helps others too.

改进:我必须做类似的计算活动在我的应用程序,所以想着写这篇code只有一次。我能德titleHeight保存到一个静态变量,因此可以使用所有活动。 但

FOR IMPROVEMENT : I have to do similar calculations for all Activities in my application, so was thinking about writing this code only once. I can save teh titleHeight to a static variable so can use in all activities. BUT

可以在用户更改手机的密度在运行时。 如果是这样,那么活动的onCreate将被再次或不叫? 如果没有,那么我可以捕获密度变化事件在那里我可以添加此code,使当前活动进行刷新。

Can the user change the phone's density at runtime. If so, then the Activity's onCreate will be called again or not ? If not, then can I trap the density change event where I can add this code and make the current activity to refresh.

任何对于提高想法的建议是AP preciated。

Any idea suggestions for improving is appreciated.

这篇关于安卓:获取屏幕尺寸为根布局只的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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