Android knowledge point

Source: Internet
Author: User

1. Use layout_weight to implement adaptive screen

 

I. Details about layout_weight's most recommended Android multi-screen adaptive solution. This attribute determines the display weight of the control in its parent layout. It is generally used in linear layout. The smaller the value, the higher the priority of the corresponding layout_width or layout_height. In general horizontal layout, the priority of layout_width is determined. In vertical layout, the priority of layout_height is determined. The traditional layout_weight method is to set the layout_width and layout_height of the current control to fill_parent, so that the display ratio of the control can be completely handed over to layout_weight; in this way, the smaller the value of layout_weight, the larger the display ratio. However, if there are too many controls and the display ratio is not the same for the two controls, it will be difficult to control them. After all, the inverse ratio is not so good. So now the most popular 0 PX value method. The seemingly incomprehensible layout_height = 0px method, combined with layout_weight, can display controls in a proportional manner, easily solving one of the most headaches of Android development.First read the following styles (style_layout.xml)

 

?
Code snippet, double-click Copy
0102030405060708091011121314151617181920212223242526 <?xml version="1.0" encoding="utf-8"?>                                          
<resources>   <! -- Full screen stretch -->  <style name="layout_full">      <item name="android:layout_width">fill_parent</item>      <item name="android:layout_height">fill_parent</item>    </style>   <! -- Fixed its own size -->  <style name="layout_wrap">      <item name="android:layout_width">wrap_content</item>      <item name="android:layout_height">wrap_content</item>    </style> <! -- Horizontal distribution -->  <style name="layout_horizontal" parent="layout_full">      <item name="android:layout_width">0px</item>    </style>     <! -- Vertical Distribution -->  <style name="layout_vertical" parent="layout_full">      <item name="android:layout_height">0px</item>    </style>          </resources>  

 

As you can see, the layout_width and layout_height attributes are encapsulated into four styles. select one of them based on the actual layout and do not need to be set by yourself, those who have read the demo of my previous activitygroup should be very familiar with it, and the layout of my demo is as follows (weight_layout.xml)

 

?
Code snippet, double-click Copy
010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748 <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"        style="@style/layout_full"        android:orientation="vertical">        <LinearLayout                 style="@style/layout_vertical"                android:layout_weight="1"                android:orientation="horizontal">                 <View                         style="@style/layout_horizontal"                         android:background="#aa0000"                         android:layout_weight="1"/>                 <View                         style="@style/layout_horizontal"                         android:background="#00aa00"                         android:layout_weight="4"/>                 <View                         style="@style/layout_horizontal"                         android:background="#0000aa"                         android:layout_weight="3"/>                 <View                         style="@style/layout_horizontal"                         android:background="#aaaaaa"                         android:layout_weight="2"/>                         </LinearLayout>         <LinearLayout                 style="@style/layout_vertical"                android:layout_weight="2"                android:orientation="vertical">                <View                         style="@style/layout_vertical"                         android:background="#ffffff"                         android:layout_weight="4"/>                         <View                         style="@style/layout_vertical"                         android:background="#aa0000"                         android:layout_weight="3"/>                 <View                         style="@style/layout_vertical"                         android:background="#00aa00"                         android:layout_weight="2"/>                 <View                         style="@style/layout_vertical"                         android:background="#0000aa"                         android:layout_weight="1"/>         </LinearLayout></LinearLayout>


The layout of the entire interface looks very intuitive, but the nested logic should be managed by yourself. The display effect is as follows: one on the left is a X interface, and the other on the right is a x Interface (as shown in the following figure). The display ratio is exactly the same as that in the code, I won't talk much about it. You can see it by comparison.

 

2. timer and timertask to regularly update a program

Timer timer = new timer ("");

Timer. Schedule (New timertask () {}, date, long peroid );

Date indicates the time to run, and peroid indicates that the task runs once after a period of time. If you only want to run the task once, do not define the long peroid parameter.

 

3. ANR --- the program does not respond

This will be in data/ANR/trace. txt. We can analyze this file to see why this problem occurs, as shown in:

Including the time when the file appeared, and the package in which the file appeared, and the. Java file, it is easy to locate. Check whether I/O Memory leakage is blocked.

Related Article

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.