In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "what are the Android layout interview questions?" in the operation of the actual case, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
What are the layouts commonly used in Android?
FrameLayout
RelativeLayout
LinearLayout
AbsoluteLayout
TableLayout
GrideLayout (launched by Android 4.0)
What's the difference between Padding and Margin in UI?
The difference between android:padding and android:layout_margin is actually very simple. Padding describes the question from the point of view of the parent view. It stipulates that the content of the question must be the distance from the boundary of the parent view. Margin describes the problem from its own point of view, specifying the distance between itself and other view (up and down). If there is only one view at the same level, then its effect is basically the same as padding.
How to use weights to make the width of a control be 1 / 3 of the parent control?
You can set weightSum to 3 in the horizontal LinearLayout, and then set the weight of its child control to 1, so that the child control is the parent control's 1Acer 3.
What are the layout optimization measures in Android?
This issue is also part of Android performance optimization.
1. Minimize the nesting level of the layout
You can use the hierarchyviewer tool provided by sdk to analyze the view tree to help us find unused layouts.
2. There is no need to set an unnecessary background to avoid excessive drawing, for example, if the parent control sets the background color, and the child control completely overrides the parent control, then the parent control does not need to set the background.
3. Reuse the same layout code using tags
4. Use tags to reduce the view hierarchy
There are two main uses of this label:
1) because the root node of all Activity views is FrameLayout, we can use merge instead if our custom layout is also FragmenLayout.
2) when applying Include or ViewStub tags to import the xml structure from the outside, the imported xml can be represented by merge as the root node, so that when it is embedded in the parent structure, the subset it contains can be well integrated into the parent structure without redundant nodes.
Can only be the root element of a xml layout.
5. By realizing the delayed loading of View
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 its word objects.
About the weight algorithm of LinearLayout?
As in the code above, how to calculate the width of each Button?
Layout size = proportion of remaining space size weight + set width
What are the methods of scrollView nesting listview besides measurement?
Set the ListView height manually
After testing, it is found that directly specifying the height of ListView in xml can solve this problem, but the data in ListView is variable, and the actual height still needs to be measured.
So the method of setting the height of ListView by manual code was born.
Replace everything in ScrollView with a single ListView
If the UI design of head layout and foot layout is satisfied, use listview instead of scrollview directly.
Replace ListView with LinearLayout
Since ListView can not adapt to ScrollView, then change to a control that can adapt to ScrollView, why do you have to hang from the ListView tree?
And LinearLayout is the best choice. But what if I still want to continue to use the defined Adater? We only need to customize a class that inherits from LinearLayout and adapts it to BaseAdapter.
Custom ListView adaptable to ScrollView
This method is similar to the above method, method 3 is to customize LinearLayout to replace the function of ListView, but if my temper is stubborn, what should I do with ListView?
Then you have to customize a class that inherits from ListView and overrides its onMeasure method to adapt to ScrollView.
What is the relationship between dp and px?
Dp: short for dip, it refers to density-independent pixels. Refers to an abstract pixel that the program uses to define interface elements. A density-independent, logical size 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 DIP equals 1. 5 physical pixels.
When laying out, it is best to use dp to define the interface of our program, because this ensures that our UI can be displayed properly on screens of all resolutions.
/ * change from px (pixel) unit to dp * / public static int px2dip (Context context, float pxValue) {final float scale = context.getResources () .getDisplayMetrics () .density; return (int) (pxValue / scale + 0.5f) according to the resolution of the phone. } / * change from dip units to px (pixels) * / public static int dip2px (Context context, float dpValue) {final float scale = context.getResources (). GetDisplayMetrics (). Density; return (int) (dpValue * scale + 0.5f) according to the resolution of the phone;}
Therefore, you should also be able to understand the image resource files in the android project. Generally, we need to provide pictures of different sizes in the following folders.
Xhdpi: 2.0
Hdpi: 1.5
Mdpi: 1.0 (baseline)
Ldpi: 0.75
If a picture placed in mdpi is loaded into memory with a size of 10m, the size of a device image that encounters hdpi is loaded into memory is 15m.
What is screen size, screen resolution, screen pixel density?
Screen size refers to the length of the diagonal of the screen. Units are inches, 1 inch = 2.54 centimeters
Screen resolution refers to the number of pixels in the horizontal and vertical direction, in px,1px=1 pixels, usually vertical pixels, horizontal pixels, such as 1280 × 720.
Screen pixel density refers to the number of pixel points per inch, in dpi, an abbreviation for "dot per inch". Pixel density is related to screen size and screen resolution.
Android style and theme?
Style (Styles):
Android allows Look and Feel of Android applications to be defined in external style files, and you can apply the defined styles to different Views. You can define styles in the XML file and apply them to different components. Using XML to define styles, you only need to configure some common properties, and later, if you need to modify the style, you can modify it centrally.
Attribute (Attributes):
You can also apply individual attributes to Android styles, usually when customizing View.
Topic (Themes):
A theme is a style that applies to the entire Activity or application compared to a single view
How do I set the background image of Window in Acitivity to empty?
GetWindow (). SetBackgroundDrawable (null); whether the default background for android is empty.
Layout adaptation
After understanding some of the basic concepts of the above basic problems, some experiences of layout adaptation are summarized here.
There are four common sizes in Android: small (small), normal (normal), large (large), and super large (xlarge).
Common general resolution: low precision (ldpi), medium precision (mdpi), high precision (hdpi), ultra high precision (xhdpi) 1080p (xxhdpi)
Basic settings
Add child elements to the Menifest
When android:anyDensity= "true", when the application is installed on different density terminals, the program will load the resources in the xxhdpi, xhdpi, hdpi, mdpi, ldpi folders respectively.
On the contrary, if set to false, even if you have the same resources under the folder, the application will not automatically look for resources under the corresponding folder.
Adaptation scheme:
Use wrap_content, math_parent, weight wrap_content:
Set the size of the control according to the content of the control math_parent: set the size of the control according to the size of the parent control weight: weight, you can use the weight property to set the proportion of the control in the linear layout
Use relative layout to disable absolute layout
Create a different layout: each layout needs to be saved in the corresponding resource directory, named with the-suffix. For example, for a large screen (large screens), a unique layout file should be saved in res/layout-large/.
Use 9-patch PNG images: when we need to make the picture after stretching can still maintain a certain display effect, for example, can not stretch the important pixels in the picture, can not make the content area affected by stretching, we can use .9.png diagram to achieve
Android Drawable
Drawable is lightweight and easy to use. Android abstracts drawable objects as Drawable, and different graphics and image resources represent different drawable types. @ drawable is used to use drawable resources in the actual development process. For more information about Drawable, please see the picture below.
What's New in Android 5.0-use SVG Picture Resources
The full name of SVG is Scalable Vector Graphics, which is called scalable vector graphics. As opposed to a bitmap (Bitmap), SVG does not degrade the quality of the picture as a result of zooming.
Advantages:
* the perfect fit of the picture. The graphic quality of SVG image will not be lost when it is enlarged or changed in size. In this way, we greatly reduce the number of multi-resolution images needed for adaptation, and make the images fit perfectly with multiple resolutions, reduce the APK package size and improve the user experience.
* reduction in size. SVG is described in a XML file, a text format in which images are small in size and easy to modify.
* lightweight in design. In terms of design, we can modify the color of the SVG picture at will, which is very convenient for some cases that require the same image but different color pictures, only need to modify the fill color. For example, click on the different states of the picture, the background picture of the button, and so on.
Preliminary understanding of View
What is View?
To put it simply, View is the visual presentation of the Android system on the screen, which means that everything you see on the phone screen is View.
How is View drawn?
The drawing process of View starts from the performTraversals () method of ViewRoot, and finally draws a View through three processes: measure (), layout () and draw ().
Later chapters will talk about the View drawing process in detail, here is just a little bit of the basics.
How does View appear on the interface?
Views in Android are all rendered through Window, regardless of Activity, Dialog, or Toast, they all have a Window, and then manage the View through WindowManager. The communication between Window and top-level View--DecorView relies on ViewRoot.
Understanding of Android View controls
Controls in Android are roughly divided into two types of ViewGroup,View. ViewGroup manages the View as a container. The Android view is a schema similar to the Dom tree. The parent view is responsible for surveying, positioning, drawing and other operations. The reason why the findViewById method we often use is expensive because it is responsible for traversing the entire control tree from top to bottom to find View instances and to minimize the use of repetitive operations. Many of the controls in use are directly or indirectly inherited from View. For ease of understanding, see the figure below.
What's the difference between View and ViewGroup?
Android's UI interface is a combination of View and ViewGroup and their derived classes. Where View is the base class for all UI components, and ViewGroup is the container that holds these components, which itself is derived from View. For a general structure of the AndroidUI interface, see
It should be noted that the number of nesting times should not exceed 10 layers, otherwise the efficiency will be reduced. The picture above shows 3 layers.
Android View refresh mechanism?
In the Android layout system, the parent View is responsible for refreshing and the layout displays the child View;. When the child View needs to be refreshed, the parent View is notified to complete it.
Performance comparison between RelativeLayout and LinearLayout?
1.RelativeLayout will let the child View call onMeasure,LinearLayout twice. If there is a weight, it will also call the child View2 secondary onMeasure.
If the height of the sub-View of 2.RelativeLayout is different from that of RelativeLayout, it will cause efficiency problems, which will be more serious when the sub-View is very complex. If possible, try to use padding instead of margin.
3. Use LinearLayout and FrameLayout instead of RelativeLayout without affecting the level depth.
Understanding of Android UI interface architecture
Each Activity,Dialog,Toast contains a PhoneWindow object, and PhoneWindow sets DecorView as the root view of the application window. Inside is the familiar TitleView and ContentView, yes, the usual use of setContentView () is the set ContentView.
This is the end of the content of "what are the Android layout interview questions". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.