Android font size automatically adjusts to resolution

Source: Internet
Author: User

Mobile device Too many, the resolution is not the same, see most of the Internet to adapt to the font method is defined values320x480 or value-hdpi way to deal with.
Use the first kind of miserable, a lot of equipment resolution is not the same, do you want to define each kind?
Using the second type of tablet is not effective.

Finally, the code of the way convenient and quick ...


Java code

1.//Traversal Settings Font

2.public static void Changeviewsize (ViewGroup viewgroup,int screenwidth,int screenheight) {//Incoming activity top layout, screen width, Screen height

3. int adjustfontsize = adjustfontsize (screenwidth,screenheight);

4. for (int i = 0; I<viewgroup.getchildcount (); i++) {

5. View v = viewgroup.getchildat (i);

6. If (v instanceof viewgroup) {

7. Changeviewsize ((ViewGroup) v,screenwidth,screenheight);

8.}else if (v instanceof button) {//button increase this must be placed on the TextView, because the button also inherits the TextView

9. (Button) v). Settextsize (adjustfontsize+2);

}else if (v instanceof TextView) {

One. if (v.getid () = = r.id.title_msg) {//top title

((TextView) v). Settextsize (adjustfontsize+4);

}else{.

((TextView) v). Settextsize (Adjustfontsize);

15.}

16.}

17.}

18.}

19.

20.

21.//Get font size

22.public static int adjustfontsize (int screenwidth, int screenheight) {

screenwidth=screenwidth>screenheight?screenwidth:screenheight;.

24./**

25. * 1. Get the view width in the onsizechanged of the view, usually the default width is 320, so calculate a zoom ratio

Rate = (float) w/320 W is the actual width

27.2. Then when setting the font size paint.settextsize ((int) (8*rate)); 8 is the font size to be set at a resolution width of 320

28. Actual font size = default font size x rate

29. */

int rate = (int) (5* (float) screenwidth/320); I myself test this multiples more suitable, of course you can test and then modify

. return rate<15?15:rate; The font is too small to look good.

32.}

Finally in Avtivity OnCreate after the end of the call Changeviewsize on the line ... The text is big then it corresponds to the background is also large, so the recommended control background image with 9 type of picture, look comfortable.
Additionally, if you are developing an application that wants to browse on a tablet, add the manifest node in the Androidmanifest.xml file (DTD recommendations on the application node):


Java code

1.<supports-screens

2. Android:anydensity= "true"

3. Android:largescreens= "true"

4. Android:normalscreens= "true"

5. Android:smallscreens= "true"

6. Android:resizeable= "true"/>


View and ViewGroup 

View and ViewGroup

The UI interface for Android is made up of a combination of view and ViewGroup and their derived classes.

Where view is the base class for all UI components, ViewGroup is the container that holds the components, which itself is derived from view.

The View object is the base unit for the user interface in the Android platform.

The view class is the basis of a subclass of what it calls "widgets (Tools)", which provide a complete implementation of UI objects such as text input boxes and buttons.

The ViewGroup class also lays the groundwork for its subclasses, called Layouts (Layout), which provide layout schemas such as streaming layouts, table layouts, and relative layouts.

In general, the UI interface for developing Android applications does not use view and viewgroup directly, but instead uses the derived classes of these two large base classes.

The immediate subclasses derived from view are: Analogclock,imageview,keyboardview, Progressbar,surfaceview, textview,viewgroup,viewstub

The indirect subclasses derived from view are: Abslistview,absseekbar, Absspinner, Absolutelayout, Adapterview,adapterviewanimator, Adapterviewflipper, Appwidgethostview, Autocompletetextview,button,calendarview, CheckBox, CheckedTextView, Chronometer, Compoundbutton,

The direct subclasses derived from ViewGroup are: Absolutelayout,adapterview,fragmentbreadcrumbs,framelayout, Linearlayout,relativelayout, Slidingdrawer

The indirect subclasses derived from ViewGroup are: Abslistview,absspinner, Adapterviewanimator, Adapterviewflipper, Appwidgethostview, CalendarView, DatePicker, Dialerfilter, Expandablelistview, Gallery, Gestureoverlayview,gridview, Horizontalscrollview, Imageswitcher,listview,

It is noted here that ImageView is a common class for layouts with graphic effects, and Surfaceview is a very important class for game development compared to General view, and Absolutelayout, Framelayout, LinearLayout, relativelayout the direct subclasses of these viewgroup are the most basic layout elements in the Android UI layout.

  Custom controls (custom view and ViewGroup)

DV6300-T program Editing is the use of custom ViewGroup

About Custom ViewGroup knowledge, we can refer to E:\JAVA\Android\ data \ network data \ Application List sliding summary \applist test code, there will be a deeper understanding.

Let us introduce the most important methods of view and ViewGroup-

protected void OnDraw (canvas canvas): The method used for redrawing in the view class, which is the method that all view, ViewGroup, and its derived classes have, and is the most important method for the Android UI drawing. Developers can reload the method and draw their own various graphics and image effects within the overloaded method based on the parameter canvas.

protected void OnLayout (Boolean changed, int left, int top, int. right, int bottom): The method that will be called when layout changes in the view class, this method is all view, VIEWGR OUP and its derived classes have methods that overload the class to be customized when the layout changes, which is useful when implementing some effects.

protected void Dispatchdraw (canvas canvas): ViewGroup class and its derived classes have methods that are primarily used to control the drawing and distribution of sub-view, which can change the rendering of sub-view to achieve some complex visual effects , a typical example can be found in the Dispatchdraw overload of the Launcher module workspace.

Protected Boolean drawchild (canvas canvas, View child, Long Drawingtime): The method that the ViewGroup class and its derived classes have, which directly controls the drawing of a specific sub-View of a board, Overloading this method can control a specific child view.

The AddView method is used to add components to the View container. We can use this method to add components to this viewgroup.

Getchildat method This method is used to return the view at the specified location.

Note: The view in ViewGroup is counted starting at 0.

The view appears on the screen to go through measure (calculation) and layout.

onmeasure (int, int) View calls this method to confirm the size of itself and all child objects

OnLayout (boolean, int, int, int, int, int, int) Call this method when view wants to allocate size and position for all child objects

onsizechanged (int, int, int, int) Call this method when the view size changes

protected void onmeasure (int widthmeasurespec, int heightmeasurespec) describes:

Onmeasure incoming Widthmeasurespec and heightmeasurespec are not general dimensional values, but are values that combine patterns and dimensions.

It is generally based on the definitions in the XML file, and we can know the pattern and size based on these 2 parameters.

We need to get the pattern through int mode = Measurespec.getmode (Widthmeasurespec),

Use int size = Measurespec.getsize (Widthmeasurespec) to get the dimensions.

Mode A total of three cases, the value is Measurespec.unspecified, measurespec.exactly, Measurespec.at_most.

Correspondence Relationship:

-2147483648----The wrap_content----in the XML file Measurespec.at_most

1073741824----fill_parent-----in XML files measurespec.exactly

0-----measurespec.unspecified

  In general ViewGroup, we realize this:

protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {

Super.onmeasure (Widthmeasurespec, Heightmeasurespec);

Final int width = measurespec.getsize (widthmeasurespec);

Final int widthmode = Measurespec.getmode (Widthmeasurespec);

Final int count = Getchildcount ();

for (int i = 0; i < count; i++) {

In fact, for our own application to write, the best way is to remove the framework of the method, directly call View.measure (), as follows:

Generally we set ViewGroup XML layout is wrap_content, so that 2 parameters is 2147483648, then we call the following is

Make subcomponents fit their own size

Getchildat (i). Measure (Widthmeasurespec, heightmeasurespec);

And here's how.

The entire measure () process is a recursive process

The method is just a filter, and the measure () process is called at the end, or the Measurechild (child, H, i) method

Measurechildwithmargins (Getchildat (i)., h, I);

}

ScrollTo (Mcontrol.getcurscreen () * width, 0);

}

Of course we can also call Setmeasureddimension (H, L); To set the size of the viewgroup.

As for the onmeasure implementation of the view, we generally do not overwrite the method, the word is also simple, according to the need, as above, according to the 2 parameters passed to get the current pattern and size.

Of course we can also calculate the size ourselves, call setmeasureddimension settings.

ViewGroup's OnLayout implementation:

protected void OnLayout (Boolean changed, int l, int t, int r, int b) {

int childleft = 0;

Get the number of all child view

Final int childCount = Getchildcount ();

for (int i = 0; i < ChildCount; i++) {

Final View Childview = Getchildat (i);

Final int childwidth = Childview.getmeasuredwidth ();

Final int childheight = Childview.getmeasuredheight ();

Childview.layout (childleft, 0, Childleft + childwidth,childheight);

Left side of next view + one

Childleft + = Childwidth;

}

}

It's actually very simple to call the layout method to set the view position on the canvas, which can go beyond the screen width, and then we can scroll through the display.

Of course, we can also call layout to pass in the relevant coordinates to set the view display location (verified OK)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.