使用逻辑从Java设置布局参数

时间:2022-12-04 23:44:23

I know how to add Layout parameters from Java, but I need help in understanding what these numbers below are really for? Why exactly the same numbers needed?

我知道如何从Java添加布局参数,但是我需要帮助来理解下面这些数字的真正含义?为什么需要完全相同的数字?

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                ((screenWidth * 20) / 450), ((screenHeight * 30) / 450));

Here screenWidth is obtained by size.x and similar for screenHeight. Size is a Point object.

这里screenWidth是通过size.x获得的,类似于screenHeight。 Size是一个Point对象。

Point size = new Point();
getWindowManager().getDefaultDisplay().getSize(size)

Why 20/450? and 30/450? How are they important? For screen ratio?

为什么20/450?和30/450?它们如何重要?屏幕比例?

Does 450 mean anything?

450意味着什么吗?

1 个解决方案

#1


0  

It's not always necessary to provide ((screenWidth * 20) / 450) and ((screenHeight * 30) / 450) as arguments in LayoutParams's constructor. It takes width and height of the layout as arguments. For more information, you can have look at official documentation here.

并不总是需要在LayoutParams的构造函数中提供((screenWidth * 20)/ 450)和((screenHeight * 30)/ 450)作为参数。它将布局的宽度和高度作为参数。有关更多信息,您可以在此处查看官方文档。

#1


0  

It's not always necessary to provide ((screenWidth * 20) / 450) and ((screenHeight * 30) / 450) as arguments in LayoutParams's constructor. It takes width and height of the layout as arguments. For more information, you can have look at official documentation here.

并不总是需要在LayoutParams的构造函数中提供((screenWidth * 20)/ 450)和((screenHeight * 30)/ 450)作为参数。它将布局的宽度和高度作为参数。有关更多信息,您可以在此处查看官方文档。