简体   繁体   中英

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);

its the same thing but it forces android.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM