[英]How to position the view with margin that will look same for all devices?
I'm trying to position a view in the center to make it look good by adding 130 (margin-top) programmatically, however, I'm not sure how to give other android devices such as tablets to look the same? 我正在尝试以编程方式添加130(顶部),以将视图放置在中央,以使其看起来不错,但是,我不确定如何使其他Android设备(例如平板电脑)看起来一样吗? adding 130 to the margin-top looks good on the normal android phone but will look different on a tablet.
在普通Android手机上将130添加到页边距顶部看起来不错,但在平板电脑上看起来会有所不同。 How will I able to make a logic to give a margin-top that will provide the same look on all devices?
我将如何制定逻辑以提供可以在所有设备上提供相同外观的页边空白? Some examples or tip would be great!
一些例子或技巧会很棒! I would love to hear from you!
我希望收到您的来信!
margin_top = 130 // How to make this look good on all devices?
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
p.setMargins(0, margin_top, 0, 0);
v.requestLayout();
My approach to this is the following: 我的处理方法如下:
1). 1)。 Get the device height.
获取设备高度。 Also check this answer.
同时检查此答案。
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
2). 2)。 Calculate the ratio.
计算比率。
You have set a top margin of 130dp
I assume. 我假设您已将最高边距设置为
130dp
。 Using pixplicity I managed to convert it to px
. 使用pixplicity我设法将其转换为
px
。
Your phone has a resolution height x weight in pixels. 您的手机的分辨率高度x像素权重。 Let's define height as
phoneHeight
. 让我们将height定义为
phoneHeight
。
Here it becomes a little tricky for me too. 在这里我也变得有些棘手。 As you can see 130dp has different pixel values for ldpi, mdpi, etc. I personally don't know which one you should use, you might try the mdpi one.
如您所见,130dp对于ldpi,mdpi等具有不同的像素值。我个人不知道应该使用哪一种,您可以尝试使用mdpi。 (I'll try to research more and come back with an answer).
(我将尝试进行更多研究并返回答案)。
LE: LE:
You can use this method to convert dp to pixels according to your phone. 您可以使用此方法根据手机将dp转换为像素。 (See this answer).
(请参阅此答案)。
public static float convertDpToPixel(float dp, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
return px;
}
Use the returned px into the formula below. 将返回的px用于以下公式。 (let's call the returned px -
convertedPx
. (让我们调用返回的px-
convertedPx
。
3). 3)。 The formula for the top margin's value.
最高保证金价值的公式。
If we use the value of 130dp for mdpi from the table we get 130px. 如果我们从表中将130dp的值用于mdpi,则会得到130px。 So the formula would be:
因此公式为:
margin_top = (convertedPx / phoneHeight) * height
with phoneHeight
the height of the phone you are currently testing on and height
the height of the device using displayMetrics
. 与
phoneHeight
手机的高度,你目前正在测试并height
使用该设备的高度displayMetrics
。
NOTICE: convertedPx
will be a constant. 注意:
convertedPx
将是一个常量。 You only have to run this method ( convertDpToPixel
) on your phone to find out how much 130dp mean in pixels. 您只需在手机上运行此方法(
convertDpToPixel
)即可找出130dp的像素平均值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.