alexbirkett
alexbirkett

Reputation: 2682

Specify icon size as a function of font size

The app design that I'm implementing requires icon sizes to be a function of font size. The height of the icon should roughly match the height of the font.

It appears to be possible to specify any view size in sp.

Google has decreed that layout sizes should not be specified in sp but is there anything to stop me specifying an ImageView size in sp? It appears that nobody else is doing this and I'm worried about falling into unforeseen traps.

Upvotes: 0

Views: 630

Answers (2)

alanv
alanv

Reputation: 24114

Yes, you can safely specify the ImageView size in sp. This is not recommended for layouts because they typically scale to fit their content and specifying their size in sp would a) be unnecessary and b) probably look bad.

Upvotes: 0

Stewart
Stewart

Reputation: 496

You could use the align options to size the icon

android:layout_alignTop="@+id/your_font_view" android:layout_alignBottom="@+id/your_font_view"

and set android:scaleType to the appropriate value.

It would probably be best to be in a RelativeLayout as well

From android dev site

"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)."

Upvotes: 1

Related Questions