Today, someone asked me, Android system different resolution, different size of the phone, font size how to adapt it? In fact, the adaptation of the font and picture adaptation is a truth.
One
The principle is as follows:
Suppose you need to adapt to 320x240,480x320 resolution. Create a new folder under the Res directory values-320x240, values-480x320. Then in the folder values, values-320x240 and values-480x320 new XML file Dimens.xml, the XML file content is as follows:
The Dimens.xml content under vaules-320x240 is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><resources><dimen name= "Btntextsize" >18sp</dimen> </resources>
the Dimens.xml content under values-480x320 is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><resources><dimen name= "Btntextsize" >18sp</dimen> </resources>
For phones with different resolutions, the Android system automatically fits the size values of the fonts that are loaded in the corresponding file.The value of the btntextsize is different。 The following methods are referenced in the layout file:
<textview android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:gravity= "center" Android:id= "@+id/lblset" style= "@style/btntext" android:textsize= "@dimen/btntextsize" ></TextView>
This is called in the Java file:
int size= (int) this.getresources (). Getdimension (R.dimen.text_size);
This way, you can easily set the size of the font at different resolutions. Of course, not only font size, width and other properties of the other, can also be set in a similar way!
Two
< Span style= "Color:rgb (68,68,68); font-size:16px; Line-height:24px ">1. Get the view width in the onsizechanged of the view, generally the default width is 320, so calculate a zoom ratio
rate = (float) w/320 W is the actual width
2. Then when you set the font size paint.settextsize ((int) (8*rate)); 8 is the font size to be set at 320 resolution width BR style= "Color:rgb (68,68,68); font-size:16px; line-height:24px "> actual font size = default font Size x rate
<textview android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "@string/hello" android:textsize= "@dimen/text_size" />
layout Multi-resolution adaptation:
Technical advice: Please pay attention to the subscription number, name: Non-famous programmer, No.: Smart_android
How is the Android font size adaptive to different resolutions?