简体   繁体   English

如何在所有设备上看起来相同的边距处放置视图?

[英]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

dp到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.

相关问题 如何让我的应用在所有设备中看起来都一样? - How to make my app look the same in all devices? 如何使应用程序在所有设备上看起来都一样 (android) - How to make a app scale the look the same on all devices (android) 调整图像大小以使所有设备看起来相同 - Resizing an image to look the same across all devices 图标在所有设备上看起来都不一样 - Icons don`t look the same on all devices 如何设置在不同密度设备上看起来相同的视图位置 - How to set View position that looks same on different density devices 如何将图像定位在所有android设备的同一位置? - How to position images at the same spot all android devices? 在所有设备上保持相同的视图 - Keep view the same on all devices 如何使文本在所有设备中的长度(宽度)和高度看起来都一样 - How to make Text to look like same with respect to length (width) and height in all devices 如何保持图像XY比例不变,并使其在所有设备上看起来都一样? - How to keep image XY scale ratio constant and make it look the same on all devices? Android如何评估视图大小以在所有设备中保持相同比例 - Android how to evaluate view size to keep same proportion in all devices
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM