First line of code-notes (4)

Source: Internet
Author: User
Tags transparent color

Units and Dimensions

PX is the meaning of pixels, which is the smallest element that can be displayed on the screen, and any visible object in our application is made up of dots of pixels. A single pixel is very tiny and invisible to the naked eye. Different resolution of the same number of pixels on the phone account for the same size

DP is the meaning of the density-independent pixels, also known as dip, and the ratio of its display in different density screens will be consistent compared to PX.

The SP is the meaning of the scalable pixel, it adopts the same design concept as DP, solves the problem of the adaptation of the text size.

Density is the number of pixels per inch of the screen, usually in DPI. For example, a mobile phone screen width is 2 inches long is 3 inches, if its resolution is 320*480 pixels, then the density of this screen is 160dpi, if its resolution is 640*960, then this screen density is 320dpi, so the higher the density value of the screen display more fine effect.

To dynamically get the density value of the current screen:

float xdpi = getresources (). Getdisplaymetrics (). xdpi; float ydpi = getresources (). Getdisplaymetrics (). ydpi; LOG.D ("mainactivity", "xdpi is" + xdpi); LOG.D ("mainactivity", "ydpi is" + ydpi);

160dpi on the screen, 1DP equals 1px, and on the 320dpi screen, 1DP equals 2px. Most use DP to specify the width and height of the control

Making Nine-patch Pictures

In the Android SDK directory, there is a tools folder where you find the Draw9patch.bat file in this folder.

A small black dot is drawn on the four border of the picture, and the part drawn in the top and left borders represents the area where the black mark is stretched when the picture needs to be stretched, and the portion drawn in the bottom and Right boxes indicates the area where the content will be placed

The Android:divider property in the ListView lets you specify the color of the ListView divider, #0000 indicates that the divider is set to a transparent color.

The understanding of QQ Chat Interface:

1: Actually a ListView,

2: Draw the chat bubble box with. 9 Figure

3: Each line has two direction of the chat box, can be in the adapter GetView () method by judging the message type with . setvisibility (view.visible); . setvisibility (view.gone); Controls whether bubbles are visible

4:adapter.notifydatasetchanged (); Refresh the display in the ListView when there is a new message

Msglistview.setselection (Msglist.size ()); Position the ListView to the last row

Inputtext.settext (""); Empty the contents of the input box

Fragment

Replace one fragment with another fragment, using framlayout, such as a framlayout with only one right_layout, to replace with anotherrightfragment, you can use the following code:

New== fragmentmanager.begintransaction (); Transaction.replace (R.id.right_layout, fragment); Transaction.commit ();

Framlayout ... Well...

Press the back key to return to the previous fragment:

Fragmenttransaction provides a addtobackstack () method that can be used to add a transaction to the return stack, which can receive a name that describes the state of the return stack, typically passing in null.

Add Transaction.addtobackstack (NULL) to the previous line in Transaction.commit ();

Communication between the fragment and the activity

are in a separate class, and there is no obvious way to communicate directly with each other.

Fragmentmanager provides a method similar to Findviewbyid () that is specifically used to get the fragments from the layout file, that is, the Findfragmentbyid () method that calls Fragmentmanager, You can get an instance of the corresponding fragment in your activity, and then you can easily invoke the method in fragment.

Rightfragment rightfragment = (rightfragment) Getfragmentmanager (). Findfragmentbyid (r.id.right_fragment);

In each fragment, you can call the Getactivity () method to get the active instance associated with the current fragment:

Mainactivity activity = (mainactivity) getactivity ();

The idea of communication between fragment:

The activity associated with it can be obtained first in a fragment, and then the activity is used to obtain an instance of the other fragment.

Temporary wood has code, later used to re-check, digging a pit after filling ...

Fragment Status and callbacks

1. Running state when a fragment is visible and the activity it is associated with is in a running state, the fragment is also running.

2. Paused state when an activity enters a paused state (because another activity that is not fully occupied by the screen is added to the top of the stack), the visible fragments associated with it go to the paused state.

3. Stop state when an activity enters a stopped state, the fragments associated with it go to the stopped state. or by calling Fragmenttransaction's remove (), replace () method to remove the fragment from the activity, but with the Addtobackstack () method called before the transaction commits, the fragment also goes to the stopped state. In general, fragments entering the stop state are completely invisible to the user and may be reclaimed by the system.

4. Destroying a state fragment is always dependent on the activity, so when the activity is destroyed, the fragments associated with it are entered into the destruction state. or by calling Fragmenttransaction's remove (), replace () method to remove the fragment from the activity, but before the transaction commits, the Addtobackstack () method is not called . The fragments will also go into the destruction state.

There are several callback methods in the activity, almost all in fragment, but fragment also provides some additional callback methods:

1. Onattach ()
Called when the fragment is associated with an activity.
2. Oncreateview ()
Called when a view is created for fragmentation (load layout).
3. onactivitycreated ()
Make sure that the activity associated with the fragment must have been created at the time of the call.
4. Ondestroyview ()
Called when the view associated with the fragment is removed.
5. Ondetach ()
Called when fragmentation is associated with an activity.

Fragments complete life cycle can be referenced, images from the Android official website.

When Rightfragment is loaded on the screen for the first time, it is executed in turn

Onattach (),

OnCreate (),

Oncreateview (),

Onactivitycreated (),

OnStart (),

Onresume ()

Then click on the button in the Leftfragment, because Anotherrightfragment replaced the rightfragment, at this time the rightfragment into a stop state, so

OnPause (),

OnStop (),

Ondestroyview ()

Of course, if the Addtobackstack () method is not called at the time of substitution, then the rightfragment will enter the destruction state,

OnDestroy ()

Ondetach ()

Then press the back key, Rightfragment will return to the screen, because rightfragment back to the running state, so

Onactivitycreated (),

OnStart (),

Onresume ()

Note that the OnCreate () and Oncreateview () methods do not execute at this point because we have used the Addtobackstack () method to make the rightfragment and its views not destroyed. Press the back key again to exit the program, and then execute the

OnPause (),

OnStop (),

Ondestroyview (),

OnDestroy (),

Ondetach () Eventually destroys the activity and debris together.

You can save the data through the Onsaveinstancestate () method, and the saved data can be re-obtained in the three methods of OnCreate (), Oncreateview (), and onactivitycreated (). They all contain a savedinstancestate parameter for a bundle type.

First line of code-notes (4)

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.