android - 自顶向下布局

标签 android android-layout tablelayout

对于一个应用程序,我想从上到下设计布局:从全屏尺寸开始,将屏幕尺寸分成网格并将控件放置在该网格中的固定位置等。对我来说,这似乎是设计适合几乎所有屏幕尺寸的应用程序的最简单方法。理想情况下,我也想缩放其他东西:Cornerroundings、fontsizes 等。

到目前为止,我的印象是 Android 不想支持这种方法:几乎所有东西都有缩放机制,但它很快就会变得丑陋: LinearLayout 和 TableLayout 支持权重,因此我可以很好地使用它们,但如果我想要随屏幕尺寸缩放的边距,我需要大量嵌套布局。 这有一些设计原因吗? 实现类似于 AbsoluteLayout 的布局应该非常简单,但所有内容都基于可用屏幕大小的分数,但不存在这样的东西。

为什么不呢?人们不想要这样的东西是出于某种原因,还是只是还没有人费心去实现它?

最佳答案

对于随屏幕尺寸缩放的布局,请勿使用像素来指定尺寸。请改用“dp”单位(或倾角)。这个单位是密度独立的,所以当你让你的布局完全适合模拟器屏幕尺寸时,Android 会为你处理缩放,它适用于小至廉价手机 (320x240px) 的屏幕到大至10 英寸平板电脑。您当然需要提供各种分辨率的图像,因为图像缩放总是很快变得非常糟糕。

来自官方 Android 开发者网站:

Defining layout dimensions with pixels is a problem because different screens have different pixel densities, so the same number of pixels may correspond to different physical sizes on different devices. Therefore, when specifying dimensions, always use either dp or sp units. A dp is a density-independent pixel that corresponds to the physical size of a pixel at 160 dpi. An sp is the same base unit, but is scaled by the user's preferred text size (it’s a scale-independent pixel), so you should use this measurement unit when defining text size (but never for layout sizes).

在同一页面中,他们展示了如何为各种屏幕密度提供图像:

http://developer.android.com/training/multiscreen/screendensities.html

关于android - 自顶向下布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13961736/

相关文章:

android - 制作自定义单选按钮,图像位于中心

android - 在 Android 中更改 ActionMode 背景颜色时遇到问题

android - 在 Android 中支持多屏幕

android - 加权按钮上的文本不占用与按钮相同的空间

android - 缺少复选框

android - 通知图像移动到另一个文件夹

android - Huawei AppGallery Connect API - 403 客户端 token 授权失败

android - 如何将 RelativeLayout 添加为菜单项 - Android

android - getTop() 为 TableLayout 中的所有 View 返回 2

android - 使用 TableLayout.LayoutParams 为 3 个 TextView 分配权重