How can I get pixel perfect layouts on Android devices?
I have a custom component in my app where I extends a ViewGroup and add a bunch of rows (LinearLayouts) with data and a small border between them. This looks really good on HVGA screens but it looks terrible on QVGA screens.
My problem is the parts where I want a 1 pixel border which works fine on HVGA but on QVGA some of the borders becomes either 2 pixels or 0 pixel (its only one of the two but I've seen both on different screen sizes). I make the border by having a black linear layout which I set padding to.
I guess the problem has something to do with px vs dp as I seen the same thing before when I used px in my layout. But 开发者_C百科now that I'm trying to set the border size in code I can only put px, is there any way around this?
I'm aware about the calculation you can do to get dp values in code but that doesn't really help me as I have to set integer values (so calculating the border will give me 1 or 2 for each row and it will still look bad).
In general, i strongly recommend having all possible layout values refer to a resource value. Sizes are going to be stored as "values". These values will accept dp, the ideal thing to use.
If your layout cannot be general enough to fit all screens, then customizing different sizes for different screens is super easy by adding additional value resources.
精彩评论