Android – Am I Doing this Right?

I am building an android app that requires a layout to be added programmatically. This is the line that seems to be giving me a hard time:

RelativeLayout.LayoutParams rightScreenParams = new RelativeLayout.LayoutParams(
            (int) (screenWidth * 0.25), (int) (screenHeight * 0.94));

The variables screenWidth and screenHeight are the width and height of the screen in pixels. The layout is supposed to be 25% of the width of the parent layout, and 94% of the height. When I add the parameters to the layout and then add the layout, it takes up the entire screen instead of the dimensions I mentioned.

So am I using this constructor correctly? Any help is appreciated. Thank you for your time.

Edit:

I get screenWidth and screenHeight in onCreate():

DisplayMetrics displayMetrics = new DisplayMetrics();
    WindowManager wm = (WindowManager) getApplicationContext()
            .getSystemService(Context.WINDOW_SERVICE);
    wm.getDefaultDisplay().getMetrics(displayMetrics);
    screenWidth = displayMetrics.widthPixels;
    screenHeight = displayMetrics.heightPixels

The variable screenHeight is returning as 1184 even though the actual height in pixels is 1280. The variable screenWidth seems to be fine. So how can I get an actual screen height in pixels?


RelativeLayout.LayoutParams rightScreenParams = new RelativeLayout.LayoutParams(
           RelativeLayout.LayoutParams.Wrap_Parent,samehere);
View.setLayoutParams(rightScreenParams);//your view
//after you add it to the layout then you call
rightScreenParams.width = (int) (screenWidth * 0.25);
rightScreenParams.height = (int) (screenHeight * 0.94);

它同样的事情,但它迫使android。

链接地址: http://www.djcxy.com/p/90662.html

上一篇: 什么是在Android上获取用户当前位置的最简单最稳健的方式?

下一篇: Android - 我是否正确地做?