开发者

Should I use dip, dp, sip or sp for text formatting?

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 dro开发者_如何学Goid 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?


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜