Android app development-data storage and interface presentation (i) (re-plate)

Source: Internet
Author: User

Common layouts

Relative layout (relativelayout)

The control's default position in relative layout is the upper-left corner (left-justified, top-aligned parent element), and controls can overlap

can be aligned relative to the parent element, centered horizontally, vertically, vertically, and horizontally relative to the parent element

Android:layout_alignparentright= "true"    <!----Android:layout_ Centerhorizontal= "true"   <!-- -

Can be left and right aligned relative to other controls

android:layout_alignright= "@id/tv1"    <!-- set right-aligned with the specified control  

Can be laid out on top, bottom, left, right of other controls

android:layout_torightof= "@id/tv1" <!----android:layout_below= "@id/tv1" 
    <!-- --

Linear layout (LinearLayout)

Linear layout has a layout direction, horizontal (horizontal) or vertical (vertical)

<!-- -

In the vertical layout, set the left and right alignment, the horizontal center will take effect, the other is not valid; Under horizontal layout, set top, bottom, vertical center to take effect, other invalid

When the width or height of a control is used, it is possible to match_parent other controls out of the screen

Weight: Distributes the remaining width or height of the screen proportionally

android:layout_weight= "1"  <!-- --        

Frame Layout (framelayout)

The default position of the control under the frame layout is also the upper-left corner (left-justified, top-aligned parent element), and controls can overlap-relative layout

You can set up or down to align horizontally vertically, but not with other control layouts--linear layout

Table Layout (tablelayout)

The nodes in the table layout can not be set to a wide height because the settings are also invalid. The child node of the root node tablelayout is the same as the parent element, the content of the TableRow node is the width of the parcel, and the content of the package is high. Each TableRow node is a row, and each of its child nodes is a column

The following properties can be set in the root node, which means that the 1th column stretches to fill the remaining space of the screen width

android:stretchcolumns= "1"

Absolute Layout (absolutelayout)

directly specifies the x and y coordinates of the control, which is basically not used

android:layout_x= "144DP" android:layout_y= "154DP"

Logcat

Log information is divided into 5 levels

    verbose: redundancy, lowest level

    Debug : Debugging

    Info : Normal level information, the log level of the System.out.print output is info

    warn: warning

    Error : wrong, highest level

The Log tool class in Android is log (Android.util.Log), and Android provides the following log output API for us to print the logs. The first parameter is tag, usually passed in the current class name, mainly used to filter the printing information, we can define a filter to easily view the log information; The second parameter is what we want to print.

LOG.V (Tag, "Come on, kids shoes" "Come on,kids Shoes" "Come on,Kids Shoes" "Come on, kids shoes" "Come on, Kids shoes");

Storage for Android

Internal storage space

RAM memory: Running memory, equivalent to the computer's memory

ROM Memory: Storage memory, equivalent to the computer's hard disk (this is the internal storage space, is necessary)

External storage space

SD card: The equivalent of a computer's mobile hard drive (optional)

Before Android 2.2, SD card path: SDcard

Before Android 4.3, sd card path: Mnt/sdcard

Android 4.3 start, sd card path: Storage/sdcard

To be compatible with low-version programs, the Android system retains a "shortcut" to the location of the original SD card

Now buy mobile phones, such as Meizu MX5 16G version, this 16G actually refers to the external storage space, and manufacturers did not tell us the phone's internal storage space is how much

Store read and write data on-premises

Using APIs to get the true path of internal storage

Getfilesdir () Gets the path of the file object is Data/data/[package name]/files, which is stored in this path files, as long as you do not delete, it has been in

Getcachedir () The path to the resulting file object is Data/data/[package Name]/cache, which is stored under this path, and may be deleted when memory is low

In the System Management application interface Click Clear Cache, will clear the cache folder of data, click to clear the data, will erase the entire package record data

Read and write data on external storage

The simplest way to open an SD card

New File ("Sdcard/xxx.txt");

Use the API to get the true path of the SD card, as some phone manufacturers change the path of the SD card

New File (Environment.getexternalstoragedirectory (), "Xxx.txt")

Determine if the SD card is ready

if (Environment.getexternalstoragestate (). Equals (environment.media_mounted))

SD Card main several states

Media_unknown: SD Card not recognized

media_removed: no SD card

MEDIA_UNMOUNTED:SD card exists but not mounted

MEDIA_CHECKING:SD card is being prepared.

MEDIA_MOUNTED:SD card is already mounted and available

Write SD card requires permission

<!---<android:name= "Android.permission.MOUNT_ Unmount_filesystems "/><!---<  Android:name = "Android.permission.WRITE_EXTERNAL_STORAGE" />

Read SD card, do not need permission before 4.0, 4.0 can be set as required if permissions are set

<android:name= "Android.permission.READ_EXTERNAL_STORAGE"/>

Find the code that gets the remaining capacity of the SD card from the Android source

Import Settings Project

Find "free space" to get

<name= "memory_available"  msgid= "418542433817289474"  >"free space"</string>

Find "memory_available" and get

<android:key= "Memory_sd_avail"  style= "? android:attr/ Preferenceinformationstyle "  android:title=" @string/memory_available "  android:summary  = "xx"/>

Find "Memory_sd_avail" and get

This string is the SD card remaining capacity formatsize (Availableblocks * blockSize) + readOnly  These two parameters multiply, Get the remaining capacity of the SD card in bytes availableblocks * blockSize

The storage device is divided into chunks, each with a fixed size

Chunk Size * Number of chunks equals the total size of the storage device

File access Permissions

In Android, every app is a standalone user

Access to the file refers to who can access the file (clip), using these 10 letters to indicate: DRWXRWXRWX

First Letter:

D: Indicates folder

-: Indicates file

First set of rwx: Represents the file owner (owner) 's permissions on the file

R:read, read

W:write, write

X:execute, Executive

Second set of rwx: Represents the permissions of the user who belongs to the same user group (group) as the owner of the file

Third group rwx: Represents the permissions of other users (other) to the file

Sharedpreference

Sharedpreference is ideal for storing scattered, simple data, primarily for storing various configuration information of the application

Write data to Sharedpreference.

Sharedpreferences sp = getsharedpreferences ("config", mode_private); // got a Sharedpreference object . Editor ed = Sp.edit ();    // get the editor ed.putstring ("name", "ENIAC");    // Write Data ed.commit ();       

Fetch data from Sharedpreference.

Get a Sharedpreference object  to fetch data from a sharedpreference.

Generate an XML file backup SMS

Create a few virtual SMS objects that exist in list

Backup data is usually backed up to an SD card

Use StringBuffer to stitch strings

Append all nodes of the entire XML file into SB object

Sb.append ("<?xml version= ' 1.0 ' encoding= ' utf-8 ' standalone= ' yes '?>"); Sb.append ("<smss>");    // Add SMSS Start node ....

Write SB into the output stream

Fos.write (Sb.tostring (). GetBytes ());

To generate an XML file using an XML serializer

Get the XML Serializer object

XmlSerializer xs = Xml.newserializer ();

Set the output stream to the serializer

New File (Environment.getexternalstoragedirectory (), "Backupsms.xml"new"Utf-8");    // specifying a good output stream for the serializer

To start generating an XML file

true ); Xs.starttag (null, "SMSS");

Pull Parse XML file

The original XML resource is generally saved under the/res/xml/path

Write a weather.xml file yourself, save some weather information

Gets the parser that resolves the resource, based on the ID of the XML resource

Xmlpullparser XP = Getresources (). GETXML (R.xml.weather);

Start parsing

Get the event type of the current node where the pointer is located

int type = Xp.geteventtype ();

There are five main types of events

Event types for Start_document:xml headers

Type of event End_document:xml tail

Start_tag: Event type of the start node

End_tag: Event type for end node

Text: The event type of the literal node

If the obtained event type is not end_document, then the parsing is not complete, and if it is, the parsing is complete and the while loop ends

 while (Type! = xmlpullparser.end_document)

When we resolve to different nodes, we need to do different things, so we can judge the name of the current node.

When parsing to the start node of weather, the new list

When resolving to the start node of city, the city object is created and the object is created to make it easier to save the text that will be parsed

When parsing to the name start node, gets the text content of the next node, same as temp, PM

 CaseXmlpullparser.start_tag://gets the name of the current node    if("Weather". Equals (Xp.getname ())) {Citys=NewArraylist<city>(); }Else if("City". Equals (Xp.getname ())) { City=NewCity (); }Else if("Name". Equals (Xp.getname ())) {//gets the text of the next node of the current nodeString name =Xp.nexttext ();  City.setname (name); }Else if("Temp". Equals (Xp.getname ())) {String temp=Xp.nexttext ();  City.settemp (temp); }Else if("PM". Equals (Xp.getname ())) {String pm=Xp.nexttext ();  CITY.SETPM (PM); } Break;

When resolving to the end node of city, it shows that the city's three child nodes have all been parsed, adding the city object to the list

 Case Xmlpullparser.end_tag: if ("City". Equals (Xp.getname ())) {Citys.add (city);}

Android app development-data storage and interface presentation (i) (re-plate)

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.