如何在所有设备上看起来相同的边距处放置视图? [英] How to position the view with margin that will look same for all devices?
问题描述
我正在尝试通过以编程方式添加130(顶部)来将视图放置在中央,以使其看起来不错,但是,我不确定如何使其他Android设备(例如平板电脑)看起来一样吗?在普通Android手机上将130添加到页边距顶部看起来不错,但在平板电脑上看起来会有所不同.我将如何制定逻辑以提供在所有设备上都具有相同外观的页边空白?一些例子或技巧会很棒!我希望收到您的来信!
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? adding 130 to the margin-top looks good on the normal android phone but will look different on a tablet. 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();
推荐答案
我的解决方法如下:
1).获取设备高度.还要检查此答案.
1). Get the device height. Also check this answer.
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
2).计算比率.
我假设您已将最高边距设置为130dp
.使用像素清晰度,我设法将其转换为px
.
You have set a top margin of 130dp
I assume. Using pixplicity I managed to convert it to px
.
您的手机的分辨率高度为 x ,以像素为单位.让我们将高度定义为phoneHeight
.
Your phone has a resolution height x weight in pixels. Let's define height as phoneHeight
.
这对我来说也变得有些棘手.如您所见,130dp对于ldpi,mdpi等具有不同的像素值.我个人不知道应该使用哪一种,您可以尝试使用mdpi. (我将尝试进行更多研究,然后再给出答案).
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. (I'll try to research more and come back with an answer).
LE:
您可以使用此方法根据手机将dp转换为像素. (请参见此答案).
You can use this method to convert dp to pixels according to your phone. (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;
}
将返回的px用于以下公式. (我们将返回的px称为convertedPx
.
Use the returned px into the formula below. (let's call the returned px - convertedPx
.
3).上边距值的公式.
如果从表中将130dp的值用于mdpi,则得到130px.因此公式为:
If we use the value of 130dp for mdpi from the table we get 130px. So the formula would be:
margin_top = (convertedPx / phoneHeight) * height
margin_top = (convertedPx / phoneHeight) * height
具有phoneHeight
当前正在测试的手机的高度,以及height
使用displayMetrics
的设备的高度.
with phoneHeight
the height of the phone you are currently testing on and height
the height of the device using displayMetrics
.
注意: convertedPx
将是一个常量.您只需在手机上运行此方法(convertDpToPixel
)即可找出130dp的像素平均值.
NOTICE: convertedPx
will be a constant. You only have to run this method (convertDpToPixel
) on your phone to find out how much 130dp mean in pixels.
这篇关于如何在所有设备上看起来相同的边距处放置视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!