Android face test summary Android Basics (vi)
In the previous chapter Android face test summary of Android base ListView (V) is mainly the optimization of the ListView, the principle and some basic problems.
If you have any questions during the reading process, please do not hesitate to contact us. Please specify Fuchenxuan de Blog if you need to reprint
This chapter is the "Android beauty from 0 to the best of the Road" Android Foundation will summarize the Android layout common interview questions. In fact, for the basic aspects of Android development, the regular interview is nothing more than the UI, network, database, these three aspects, this chapter summarizes the issue of classic notes, hope for the vast number of Android developers, helpful.
- Android face test summary of Android Basics six
- Layout interview Questions
- Layout adaptation
- Android drawable
- View Preliminary understanding
Layout interview Questions
- What are the common layouts in Android?
- Framelayout
- Relativelayout
- LinearLayout
- Absolutelayout
- Tablelayout
- Gridelayout (Android 4.0 launch)
-
What is the difference between Padding and Margin in the UI?
android:padding and android:layout_margin differences, in fact, the concept is very simple, padding is standing in the perspective of the parent view to describe the problem, it stipulates that its contents must be with this parent view The distance from the boundary. Margin is to stand in their own perspective to describe the problem, and other (up and down) the distance between the view, if there is only one view at the same level, then its effect is basically the same as padding.
- How do I use weights to make a control's width the 1/3 of a parent control?
You can set WeightSum to 3 in a horizontal linearlayout, and then let the weight of its child controls be 1, then the child control is 1/3 of the parent control.
- What are the optimization measures for the layout in Android?
This problem is also part of Android performance optimization.
1. Minimize nesting levels of layouts
You can use the tools provided by the SDK to hierarchyviewer analyze the view tree to help us discover unused layouts.
2, do not set unnecessary background, avoid excessive drawing such as the parent control set the background color, the child control completely overrides the parent control, then the parent control is not necessary to set the background.
3. Use <include> tags to reuse the same layout code
4. Use <merge> tags to reduce the view hierarchy
There are two main uses of this tag:
1) because the root node of all Activity views is framelayout, you can replace it with merge if our custom layout is also fragmenlayout .
2) When an application Include or ViewStub tag imports an XML structure from the outside, the imported XML can be represented as a root node, so that when it is embedded in the parent structure it is good to dissolve the subset it contains into the parent structure without the redundant nodes.
<merge>Can only be used as the root element of an XML layout.
5, by <ViewStub> implementing the View delay loading
-
the difference between android:layout_gravity and android:gravity?
The first is how the layout is laid out in its parent control, and the second is how the layout lays out the layout of its word objects.
-
about the weight algorithm of linearlayout?
<linearlayoutandroid:layout_width="210DP"android:layout_height= "Wrap_content"android:orientation="Horizontal"> <buttonandroid:layout_width="30DP"android:layout_height= "40DP"android:layout_weight="1"android:text="button1" /> <buttonandroid:layout_width="30DP"android:layout_height="40DP" android:layout_weight="1"android:text="Button2" /> <buttonandroid:layout_width="30DP"android:layout_height= "40DP" android:layout_weight="2"android:text="Button3" /> </linearlayout>
How do you calculate the width of each button, as shown in the code?
Layout size = Proportion of the remaining space size weight + set width
-
ScrollView nested ListView method What else is there to measure?
- Setting the listview height manually
The test found that directly specifying the height of the ListView in XML would solve the problem, but the data in the ListView is mutable, Actual height also requires actual measurement.
The method of setting the ListView height by manual code was born.
- replaces all content in ScrollView with a single listview
Use a listview instead of ScrollView if you have a UI design that satisfies the header and foot layouts
- replace ListView with LinearLayout
Since the ListView can not adapt to the ScrollView, then a change to adapt to the ScrollView control , why hang on a tree in the ListView?
and LinearLayout is the best choice. But what if I still want to continue using the defined Adater? We just need to customize a class to inherit from the LinearLayout, and add it to the Baseadapter adaptation.
- Customizing a ListView that adapts to ScrollView
This method is similar to the method above, Method 3 is custom linearlayout to replace Listvie W function, but if my temper is stubborn, is to use the ListView to do? The
then has to customize a class to inherit from the ListView, by overriding its Onmeasure method to achieve the effect of ScrollView adaptation.
-
What is the relationship between DP and PX?
DP: is a shorthand for dip, which refers to density-independent pixels. Refers to an abstract pixel, which the program uses to define the interface elements. A density-independent, logical dimension that is consistent with a pixel on a screen with a pixel density of 160dpi. To convert density-independent pixels to screen pixels, you can use a simple formula: pixels=dips* (density/160). For example, on a screen with a DPI of 240, 1 dips equals 1.5 physical pixels.
It is best to use DP to define the interface of our program, as this will ensure that our UI will display properly on screens of all resolutions.
/*** from px (pixel) units to DP * Depending on the resolution of the phone Public Static int Px2dip(Context context,floatPxvalue) {Final floatScale = Context.getresources (). Getdisplaymetrics (). density;return(int) (Pxvalue/scale +0.5f);}/*** from the Dip unit to PX (pixels) */According to the resolution of the phone Public Static int dip2px(Context context,floatDpvalue) {Final floatScale = Context.getresources (). Getdisplaymetrics (). density;return(int) (Dpvalue * scale +0.5f);}
As such, you should also be able to understand the image resource files in the Android project, generally we need to provide different sizes of images in the following folders.
- xhdpi:2.0
- hdpi:1.5
- mdpi:1.0 (Baseline)
- ldpi:0.75
If a picture placed in the MDPI is loaded into the memory size is 10m, then the device that encounters hdpi is loaded into the in-memory size is 15m.
-
What is screen size, screen resolution, screen pixel density?
screen size refers to the length of the screen diagonal. Unit is inch, 1 inch = 2.54 cm
screen resolution refers to the number of pixels in the horizontal portrait, in px,1px=1 pixels, which are generally vertical pixels across pixels, such as 1280x720
screen pixel density refers to the number of pixels per inch, in dpi, the abbreviation "dot per inch", pixel density and screen size and screen resolution.
-
Android styles and themes?
styles (styles):
Android allows you to define the look and feel of an Android app in an external style file, and you can apply a defined style to a different view (views). You can define styles in an XML file and apply those styles to different components. Using XML to define a style, you only need to configure some common properties, and if you need to modify the style later, you can centrally modify it.
Properties (Attributes):
You can also apply a single attribute to an Android style, which is typically customized when you customize the view.
Theme (Themes):
Themes are applied to the entire Activity or application compared to a single view
-
How do I set the background of a window in acitivity to empty?
GetWindow (). setbackgrounddrawable (null); The default background for Android is not empty.
Layout adaptation
After understanding some basic concepts of the underlying issues above, here is a summary of some of the experience with layout adaptation.
There are 4 universal sizes in Android: Small (small), normal (normal), large (large), oversized (XLarge)
Common Universal Resolution: Low Accuracy (LDPI), medium accuracy (MDPI), high accuracy (HDPI), ultra-high accuracy (XHDPI) 1080P (xxhdpi)
-
Basic Settings
Adding child elements in Menifest
Android:anydensity= "true" when the application is installed on a different density terminal, the program loads the resources in the xxhdpi, xhdpi, hdpi, MDPI, ldpi folders, respectively.
Conversely, if set to False, the app does not automatically look for resources under the folder even if it has the same resources under the folder
-
Adaptation Program:
- use Wrap_content, math_parent, weight wrap_content:
To set the size of a control based on the content of the control math_parent: sets the size of the control based on the size of the parent control weight: weight, which you can use to set the proportion of the control in a linear layout using the Weight property
-
Use relative layout to disable absolute layout
-
create different layout: each layout needs to be saved in the appropriate resource directory, and the directory is named after the suffix. For example, for a large screen (large screens), a unique layout file should be saved in res/layout-large/.
- using the 9-patch PNG Image: When we need to make the image to be displayed after stretching, for example, not to stretch the important pixels in the image, or to make the content area affected by the stretching, we can use the. 9.png diagram to achieve
Android drawable
Drawable is lightweight and easy to use, and Android abstracts objects that can be drawn into drawable, and different graphics and image resources represent different drawable types that are used in the actual development process @drawable to use drawable resources. More about drawable please see
Android 5.0 new features – Using SVG image resources
The full name of SVG is the scalable vector graphics, which is called scalable vectors. It is relative to the bitmap (Bitmap), and SVG does not degrade the quality of the image as a bitmap because it is scaled.
Advantages:
* Perfect fit for pictures. SVG images have no loss of graphics quality in the case of enlarged or altered dimensions. This allows us to significantly reduce the number of resolution images required for adaptation, and allows the picture to be perfectly adapted to a variety of resolutions, reducing the size of the APK package and enhancing the user experience.
* Reduction in size. SVG is described using an XML file, which is small in size and easy to modify.
* Lightweight design. In terms of design we can arbitrarily modify the color of the SVG image, which for some cases requires the same image but different color picture is very convenient, only need to modify the fill color can be. For example, click the different states of the slice, the background picture of the button, and so on.
View Preliminary understanding
-
What is view?
In short, view is a visual representation of the Android system on the screen, meaning that what you see on your phone screen is view.
-
How is the view drawn?
The drawing process for view starts with the Viewroot performtraversals () method, followed by measure (), layout (), and draw () three processes to finally draw a view.
The following chapters will detail the view drawing process, which is just a slight base.
-
How does the view appear on the interface?
The views in Android are rendered through window, with the activity, dialog, or toast having a window and then managing the view through WindowManager. The communication between window and top-level View--decorview is dependent on viewroot completion.
-
understanding of the Android View Control
Controls in Android are broadly divided into two types of viewgroup,view. ViewGroup as a container management view. Android view, which is similar to the DOM tree architecture. The parent view is responsible for measuring operations such as positioning drawing. The reason we often use the Findviewbyid method is expensive because he is responsible for the first step down the entire control tree to find the view instance and use it sparingly in repetitive operations. Many of the controls that are now in use are directly or indirectly inherited from the view, for easy comprehension
-
What's the difference between 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 general structure of the Androidui interface can be found in
It is important to note that the number of nesting should not be more than 10 layers, otherwise it will reduce efficiency, is 3 layers
-
Android view refresh mechanism?
In the Android layout system, the parent view is responsible for refreshing and layout the child view, and when the child view needs to be refreshed, the parent view is notified to complete
-
Relativelayout and linearlayout performance comparison?
1.RelativeLayout will make the child view call 2 times onmeasure,linearlayout when there is weight, it will also call View2 times onmeasure
2.RelativeLayout Sub-view if the height and relativelayout are different, it will raise the efficiency problem, when the sub-view is very complex, this problem is more serious. If possible, use padding instead of margin.
3. Without affecting the depth of the hierarchy, use LinearLayout and framelayout instead of relativelayout.
-
Android UI Interface Architecture Understanding
Each activity,dialog,toast contains a Phonewindow object, Phonewindow settings Decorview The root view of the app window. In the inside is familiar with the Titleview and Contentview, yes, the usual use of Setcontentview () is the set of Contentview.
The content of the view is much more, and this article is only as a preliminary understanding of the basics of Android, and there will be a special section to explain more about view.
The level is limited, if have errors and omissions, welcome to correct, criticize, if need reprint, please indicate source –http://blog.csdn.net/vfush, thank you!
Android face test summary Android Basics (vi)