I sorted out some screen adaptation problems and solutions during my development.
1. Cut the icon
For example, each item in a list has an icon, but the Icon size may not be the same. In this case, you can specify the uniform size based on the largest image. Other small icons are centered and then filled with "transparent pixels" to make the Icon size consistent, to facilitate layout and layout.
2. Screen adaptation for complex interfaces
The best adaptation method is to dynamically add and generate components using the screen percentage as the computing unit. The following uses the code creation interface as an example to describe it:
We can see that each book on this shelf contains the highlights in the upper right corner and the shadows in the lower right corner. In addition, each book occupies the screen height and width, and the distance between the book and the book is required. In this case, it is basically impossible to adapt the interface from the layout file, so you can only generate components from the code. Although it is troublesome, it is better to adapt. The specific implementation is too lengthy. The following uses a simple interface as an example.
The implementation code is as follows: MainAcivity:
Package com. wly. simplecodeui; import android. app. activity; import android. content. context; import android. OS. bundle; import android. util. displayMetrics; import android. view. windowManager; import android. widget. button; import android. widget. imageView; import android. widget. relativeLayout; import android. widget. relativeLayout. layoutParams; public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // setContentView (R. layout. activity_main); RelativeLayout layout = new RelativeLayout (this); int screenWidth = getScreenWidth (this); int screenHeight = getScreenHeight (this); ImageView iv = new ImageView (this ); // specify the width of 60%. LayoutParams params1 = new LayoutParams (int) (screenWidth * 10%), (int) (screenHeight * 0.6 )); // specify x and yparams1.leftMargin = (int) (screenWidth * 0.4)/2; params1.topMargin = (int) (screenHeight * 0.2) in the upper left corner; // set the id attribute, iv is required for relative layout. setId (0x0011); iv. setBackgroundColor (0xff123456); Button left = new Button (this); LayoutParams params2 = new LayoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT); // It is located in params2.addRule (RelativeLayout. BELOW, 0x0011); // alignment left params2.addRule (RelativeLayout. ALIGN_LEFT, 0x0011); left. setText ("" "); Button right = new Button (this); LayoutParams params3 = new LayoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT); // It is located in params3.addRule (RelativeLayout. BELOW, 0x0011); // align params3.addRule (RelativeLayout. ALIGN_RIGHT, 0x0011); right. setText ("" "); layout. addView (iv, params1); layout. addView (left, params2); layout. addView (right, params3); this. setContentView (layout);}/*** get screen width * @ return unit: px */public int getScreenWidth (Context context) {int screenWidth; WindowManager wm = (WindowManager) context. getSystemService (Context. WINDOW_SERVICE); DisplayMetrics dm = new DisplayMetrics (); wm. getdefadisplay display (). getMetrics (dm); screenWidth = dm. widthPixels; return screenWidth;}/*** get screen height * @ return unit: px */public int getScreenHeight (Context context) {int screenHeight; WindowManager wm = (WindowManager) context. getSystemService (Context. WINDOW_SERVICE); DisplayMetrics dm = new DisplayMetrics (); wm. getdefadisplay display (). getMetrics (dm); screenHeight = dm. widthPixels; return screenHeight ;}}
3. Use "Container" to place dynamic data.
Sometimes the component is dynamic, that is, only the specific type or shape of the component is known at runtime. In this case, you can set a "Container" in the layout file in advance, which can be a RelativeLayout, and obtain the component information when the program is running, then generate the component and add the component to "container. Here, the "Container" is a placeholder, which means taking a place first. The specific number of components added to the container is determined by the runtime environment. The following is an example of using "Container" as an advertisement bar component. The key is that the number of advertisements contained in this component is dynamically obtained from the server. Therefore, you can use the "Container" to placeholder and then add components to it.
4. Use transparent pixels for interface adaptation
There may be transparent pixel adaptation around a component. Take the split line of ListView as an example. Assume that the left and right sides of the split line have transparent pixels. In this case, if you simply crop the component and add Margin to the left and right for adaptation, it cannot adapt to all interfaces (although you may have used the dpToPx method ). One way is to cut the transparent pixels at both ends of the split line together, and then use layout_width = "fill_parent" to automatically scale the split line with the screen width.
5. Use of ScaleType
Sometimes, ScaleType can be used to quickly adapt ImageView when the precision requirement is not very high. FitXY is usually used, that is, stretching an image to fill the component.
6. Use of custom components
So far, I have implemented two types of custom components: rewrite system components and add some functions. The other is to encapsulate several related components into a composite component for reuse. Strictly speaking, the latter is not a custom component. If you are writing a complex custom component, you need to pay attention to its measurement and layout processes. If it is a nested component, you need to pay attention to the processing of its event transfer.
Now, let's start here today and try again later.
Mount Please retain Source: http://blog.csdn.net/u011638883/article/details/18001145
Thank you !!