我应该使用 diff、dp、sip 或 sp 进行文本格式化吗?

发布于 2024-10-04 05:10:52 字数 129 浏览 3 评论 0原文

我不会说我的应用程序文本较多......但它有时会显示比其他更长的随机字符串。它们非常适合我的 droid 2,但我知道它们不会在较小的 droid 上显示相同的效果。有谁知道格式化不同大小的文本以确保它适用于所有 Android 的正确方法?

I wouldn't say my app is text heavy...but it displays random strings sometimes that are longer than others. They fit fine on my droid 2, but I know they won't display the same on smaller droids. Does anyone know the proper way of formatting different sized text to ensure it will work on all androids?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恍梦境° 2024-10-11 05:10:52

如果你只是想让一些文本变大或变小,你应该更喜欢使用Android提供的默认样式:

<TextView ... android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView ... android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView ... android:textAppearance="?android:attr/textAppearanceSmall" />

它们都不是默认的,默认是:

<TextView ... android:textAppearance="?android:attr/textAppearance" />

它肯定不仅可以应用于TextView,还可以应用于其他意见也是如此。

If you just want to make some text bigger or smaller you should prefer using default styles provided by Android:

<TextView ... android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView ... android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView ... android:textAppearance="?android:attr/textAppearanceSmall" />

Neither of them is default, default is:

<TextView ... android:textAppearance="?android:attr/textAppearance" />

It can be surely applied not only to TextView, but to other views as well.

白色秋天 2024-10-11 05:10:52

Google 建议使用 sp 作为文本大小尺寸。您使用的布局需要能够正常工作,以便当布局缩放时,它可以在所有尺寸下尽可能地工作(您可能需要针对不同尺寸的屏幕的布局)。如果文本可能不适合,请将其换行或省略

Google recommend using sp for text size dimensions. The layouts you use will need to work so when the layout scales it works at all sizes to the best degree possible (you may need layouts for different sizes of screen). If the text may not fit, wrap it or ellipsize it

风筝有风,海豚有海 2024-10-11 05:10:52

SP 和 DP 似乎并不按您预期的方式工作。
DP假设是“显示无关”,SP的描述也是一样的;但可以缩放(大概通过“缩放”命令)。
然而,它们并不像您期望的那样工作。

例如,我有两台设备,一台 320x480 手机和一台 Nexus;它是 800x1280 但将自己描述为 sw600 (我不知道为什么)。
为了了解发生了什么,我在视图中添加了一个文本视图,它由 60 个“X”字符组成(选择它们是因为它们看起来与等宽字体的字距相同)。

textView textSize 设置为 10dp,其中 60 个完全填满 Nexus 屏幕。
然而,它们在手机上显示的大小完全相同,并且几乎形成两条线。

因此,DP 似乎与显示无关;但仅描述每个字符最大的(近似)像素数。
在 SP 中定义尺寸没有什么区别。

这不是很有帮助 - 我预计会应用某种形式的缩放,因此我可以在手机上选择合适的尺寸,并且它将针对 Nexus 进行缩放。

例如,如果我的文本行大约有 30 个字符长,并且我希望它在屏幕上填满一整行,我会将其定义为 10dp;每个 dp 是一个像素(大致)。然后,当它在 Nexus 上绘制时,每个 dp 将是两个像素,并且它会显示两倍大小。

据我所知,事情并不是这样的。这使得它毫无用处。

我唯一的解决方案,这不是一个优雅的解决方案,是仅使用dimens.xml文件中的尺寸,并有六个尺寸,每个通用宽度一个(240,320,480,600,720,800 - 任何更高的尺寸都可以)自行整理),然后设计我的代码以适合 Nexus,并使用我编写的电子表格来缩放 dimens.xml 中的任何值以获得常见宽度。
这是一种痛苦;但它有效。

除非我用错了,否则Android系统就坏掉了。

SP and DP don't seem to work the way you might expect.
DP is supposed to be "display independent", and SP is described as the same; but can be scaled (presumably by "zoom" commands).
However, they do not work as you would expect.

For example, I have two devices, a 320x480 phone, and a Nexus; which is 800x1280 BUT which describes itself as sw600 (I have no idea why).
To see what was going on, I added a textview to my view, which consists of 60 'X' characters (chosen because they look the same kerned as monospaced).

The textView textSize was set to 10dp, and 60 of these entirely fill the Nexus screen.
However, they appear exactly the same size on the phone, and form nearly two lines.

So, it appears that DP is not display independent; but merely describes the (approximate) number of pixels each character will be given at maximum.
Defining the dimension in SP makes no difference.

This is not very helpful - I was expecting that some form of scaling would be applied, so I could select an appropriate size on my phone, and it would be scaled for the Nexus.

For example, if my line of text was roughly thirty characters long, and I wanted it to fill a whole line across the screen, I would define it as 10dp; and each dp would be one pixel (roughly). Then, when it was drawn on the Nexus, each dp would be two pixels, and it would appear twice the size.

This is not how it works, as far as I can tell; which makes it pretty useless.

My only solution, and it's not an elegant one, is to only use dimensions from a dimens.xml file, and to have six of them, one for each of the common widths (240,320,480,600,720,800 - anything higher can sort itself out), then design my code to fit on the Nexus, and use a spreadsheet I've written to scale any values in dimens.xml for the common widths.
It's a pain; but it works.

Unless I'm using it wrong, the Android system is just broken.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文