简体   繁体   English

使布局在所有设备上看起来都不错

[英]Make layout look good on all devices

I am using an emulator to build my first android app. 我正在使用模拟器来构建我的第一个android应用。 When I publish my app on my phone with the realease .apk , I don't have the desired results. 当我使用realease .apk在手机上发布我的应用程序时,我没有期望的结果。

<Button
    android:text="Log In"
    android:layout_width="match_parent"
    android:layout_height="100px"
    android:layout_marginTop="482px"
    android:id="@+id/loginButton"
    android:background="#76988F"
    />

What I want is for someone to explain how you can set buttons or any other interface controls to look alike on any devices. 我想要的是让某人解释如何设置按钮或任何其他界面控件,使其在任何设备上看起来都相似。 All devices are different so let's say I put the margins like this in my XAML and boot up the emulator, I'm going to see the following interface : 所有设备都是不同的,所以假设我在XAML中放入了这样的边距并启动了模拟器,我将看到以下界面:

-----------------------
|                     |
|                     |
|                     |
|                     |
|                     |
|                     |
|                     |
|                     |
|     MyButton        |
-----------------------

But when I boot it on my device, the button will appear elsewhere : 但是当我在设备上启动它时,该按钮将出现在其他位置:

-----------------------
|                     |
|                     |
|                     |
|       MyButton      |
|                     |
|                     |
|                     |
|                     |
|                     |
-----------------------

Probably because according to the device I'm on and the emulator I tried before, the margin of x pixels won't take me to the same place. 可能是因为根据我所使用的设备和我之前尝试过的仿真器, x pixels的边距不会将我带到相同的位置。 How can I make these controls stay on a good place on all devices ? 如何使这些控件在所有设备上都放置在合适的位置?

It is kind of hard to search for that information because I don't have the name of the way to let elements stay in a fixed place. 搜索这些信息有点困难,因为我没有让元素停在固定位置的方式的名称。

Because there are many devices with so many different screen sizes, resolutions, etc., probably the main thing you have to avoid is using absolute positions when placing layout elements. 由于许多设备的屏幕尺寸,分辨率等都不同,因此可能需要避免的主要事情是在放置布局元素时使用绝对位置。 The Android SDK has some powerful structures to avoid absolute positioning (ie LinearLayout, RelativeLayout), so try working with them and instead of defining positions like "12dp", use the correct combination of layout_width, layout_height (wrap_content or match_parent) and layout_weight, which can help you to place layout elements without specifying absolute positions. Android SDK具有一些功能强大的结构来避免绝对定位(即LinearLayout,RelativeLayout),因此请尝试使用它们,而不是定义“ 12dp”之类的位置,而应使用正确的layout_width,layout_height(wrap_content或match_parent)和layout_weight组合,可以帮助您放置布局元素而无需指定绝对位置。

Seeing your code the first thing i thought is 看到您的代码,我想到的第一件事是

  • Do not use hard coded pixel values in your application code. 不要在应用程序代码中使用硬编码的像素值。

You should refer to Best Practices . 您应该参考最佳做法 As the doc says 正如医生所说

For performance reasons and to keep the code simpler, the Android system uses pixels as the standard unit for expressing dimension or coordinate values. 出于性能原因并简化代码,Android系统使用像素作为表示尺寸或坐标值的标准单位。 That means that the dimensions of a view are always expressed in the code using pixels, but always based on the current screen density. 这意味着视图的尺寸始终在代码中使用像素表示,但始终基于当前的屏幕密度。 For instance, if myView.getWidth() returns 10, the view is 10 pixels wide on the current screen, but on a device with a higher density screen, the value returned might be 15. If you use pixel values in your application code to work with bitmaps that are not pre-scaled for the current screen density, you might need to scale the pixel values that you use in your code to match the un-scaled bitmap source. 例如,如果myView.getWidth()返回10,则视图在当前屏幕上为10像素宽,但是在具有更高密度屏幕的设备上,返回的值可能为15。如果在应用程序代码中使用像素值来对于未针对当前屏幕密度预先缩放的位图,您可能需要缩放在代码中使用的像素值,以匹配未缩放的位图源。

Another mistake you made is working with px. 您犯的另一个错误是使用px。 Indeed, screens have different densities, and pixels have different sizes depending on this density. 实际上,取决于此密度,屏幕具有不同的密度,像素具有不同的大小。 So you'll probably prefer using dp, a density dependent measure unit. 因此,您可能更喜欢使用dp(密度相关的度量单位)。 As for your text sizes you'll have to use sp. 至于您的文字大小,则必须使用sp。

when you use this attribution android:layout_marginTop="482px" , you shouldn't use px. 使用此属性android:layout_marginTop="482px" ,您不应使用px。 You should use dp. 您应该使用dp。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:可以在布局上使用DP并使其在所有设备上看起来一样吗? - Android: Possible to use DP on a layout and make it look the same across devices? 如何在Android中制作响应式布局,该布局在所有尺寸的移动设备上都应看起来不错,在横向人像视图中也可以使用Tab - How to make responsive layout in Android Which should look good in all size mobile and tab also in landscape portraite view 如何让我的应用在所有设备中看起来都一样? - 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) 管理所有设备的布局 - Managing layout for 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上的图像所需的所有图像大小是什么,以使事物看起来非常好? - What are all of the images sizes that you need for images on Android to make things look really good? 使ImageView在不同设备上看起来相同 - Make ImageView to look the same on different devices 调整图像大小以使所有设备看起来相同 - Resizing an image to look the same across all devices
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM