Android UI Basics

Source: Internet
Author: User

Android UI Overview

The Android UI is made up of view and ViewGroup.

ViewGroup is not visible for organizing and typesetting view and ViewGroup.

The view user displays the content and responds to the user's actions.

You can arrange the stacking of the UI as needed, but the fewer layers you stack, the better the performance.

The Android UI can be produced in code, but the more convenient way is to define the UI in an Android XML file.

Layouts is implemented by XML

There are 2 ways to define the interface structure.

1. Defining the view structure in XML

2. Dynamically create a view structure at run time

By using XML to define the view structure, we can effectively separate the code from the interface and improve the readability of the interface structure.

How XML is written

The file of the XML must contain a root, which can be either view or ViewGroup. The interface structure is constructed by adding sub-interfaces below the nodes.

The basic writing structure of XML can be referred to http://developer.android.com/guide/topics/resources/layout-resource.html

Loading XML Resources

During the compiler phase, all XML layout files are compiled into a unified view resource.

When you need to use the layout resources, you need to load the resources into the program, the general practice is to Activity.onCreate()中做加载的资源的操作。

如果界面的文件名称是main_layout.xml,则可以如下方式加载。

 Public void onCreate (Bundle savedinstancestate) {    super.oncreate (savedinstancestate);    Setcontentview (r.layout.main_layout);    }
XML Layout Properties

Each view or ViewGroup has its own unique or inherited attributes.

Id

Each view or viewgroup has an id attribute. This property is defined by Class View. Its definition syntax is:

Android:id= "@+id/my_button"

The meaning of the @ is to instruct the XML parser to parse and expand the following content as a resource for an ID.

+ means to indicate that this is a new ID that needs to be added to the resource definition file R.java.

There are some system-custom IDs that, if you reference these system-customized IDs, do not need to add the + sign, but need to add the package namespace , which defines the syntax:

Android:id= "@android: Id/empty"
A common definition of a view or widget, and is called in code as follows:
1. Define a view and configure a unique ID
<button android:id= "@+id/my_button"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/my_button_text"/>
2. Create an instance in your code with this unique ID
Button MyButton = (button) Findviewbyid (R.id.my_button);
IDs in RelativeLayout中会显的更加的重要,因为需要通过IDs来指定其旁边的View。哪个View在自己的左边,哪个View在自己的右边,或者上面或者下面。

Layout-related parameters

In an XML file, you typically use layout_something to define the position of the view in ViewGroup

The ViewGroup class implements a nested class to extend the viewgroup.layoutparams. This inline subclass defines the type to specify the location and size of the child view.

As shown, the parent view group defines the layout parameters for its child view or child view Group

The layout parameter may not be the same for its parent node and for its child nodes, so it should be treated differently.

Each view group typically contains the width and height parameters (Layout_width and layout_height), so each view inside it needs to define both properties.

In general, we do not designate it as a width or height, and are generally written as relative, which ensures that the device adapts to a variety of screen sizes.

    • Wrap_content: Adjust size based on content size
    • Fill_parent: Maximized to reach the parent's allowed, after API level 8 name changed to Match_parent
Location of layout

View as a geometry with 4 attributes corresponding to the container to which he belongs, left, top, width and height, the units of each property are pixel

Refer to the API documentation for a number of functions to get location and view size information. GetLeft (), GetTop (), GetRight (), Getbottom ().

The obtained value is typically relative to the parent node's location and size information

Size,padding and Margins

The Size is represented by the width and height of the view.

There are 2 types of width and height, one is the measured width and measured height corresponding to the parent container, which is relative to the height and width of the parent container. can be obtained by getmeasuredwidth () and Getmeasuredheight ().

The other is the actual width and height for the entire interface, which can be used by getwidth () andgetHeight()获取。

Padding也会被计算入measure size内,Padding,顾名思义,是内容与View空间直接的间隔。可以由函数setPadding(int, int, int, int)进行设置。可由getPaddingLeft(),getPaddingTop(),getPaddingRight(),getPaddingBottom()获取相对应额值。

View does not provide the Margins property, which is typically set by ViewGroup.

The usual mode of layout

Each ViewGroup offers a unique arrangement. Here are a few common ways to see them.

Note: Although it is possible to design a complex layout by embedding other layout methods in layout, this can result in a complex layout structure. Not conducive to UI performance. Try to keep the UI nested fewer layers.

Linear Layout Relative Layout Web View
Arranges the contents into Heng lie or vertical columns, or displays scroll bars if the screen is exceeded. You can flexibly set the position of the child view, which is already in the position between the child view. Display HTML documents like a browser
Binding Data sources

If the content that needs to be displayed on the interface is dynamically obtained, it can be displayed dynamically using a view that uses adapter and inherits Adapterview.

Adapter is the bridge between the data source and the Adapterview, which gets the data from the data source and then transforms it into a set of entities, populated with the view.

The general data display mode is as follows:

List View Grid View
A single-line list that can scroll up and down Table of rows and columns that can be scrolled
Fill in the data in the adapter

You can simply bind adapter with a view that inherits from Adapterview to get data from an external data source.

Andorid also provides some subclasses that inherit from adapter to handle different data forms to create a view, here are 3 more common adapter:

Arrayadapter

When the data source is an array, you can use this adapter, by default, Arrayadapter creates a textview for each item after calling ToString ().

For example, if you want to display a string array in the ListView, you need to instantiate a arrayadapter, specify its layout, the name of each string, and the string list


Android. R.layout.simple_list_item_1, myStringArray);
The parameters are: app Context, meaning display stringTextView的layout,String列表
第二步:在ListView上设置Adapter
ListView ListView = (ListView) Findviewbyid (R.id.listview);
Listview.setadapter (adapter);

If you need to define the output format of a string, you can overload ToString (), or you can do other controls that can display rich content, such as ImageView, without TextView.

Can be easily extended ArrayAdapter getView() to meet different display styles.

Simplecursoradapter

Use this adapter if the data source is from cursor (Cursor). When using this adapter, you need to specify which row of the cursor, which column is inserted into the layout view.

For example, there is a cursor that contains the people data, people contains the name and number fields.

At this point, you need to create a string array to specify which fields need to be displayed. An array of shapes that specifies which control the field needs to be displayed on


ContactsContract.CommonDataKinds.Phone.NUMBER};
Int[] Toviews = {r.id.display_name, r.id.phone_number};

When instantiating Simplecursoradapter, these parameters are passed in


R.layout.person_name_and_number, cursor, fromcolumns, toviews, 0);
ListView ListView = Getlistview ();
Listview.setadapter (adapter);

SimpleCursorAdapterThe fromColumns content that will be filled into toViews the view.

if , during the program's cycle, if the data within the adapter is changed, then notifydatasetchanged () must be called, and the function will notify the view associated with the adapter and refresh itself.

Handling Click events

You can make a response to a click event by implementing an AdapterView.OnItemClickListener interface AdapterView .

//Create a message Handling object as an anonymous class. Private New Onitemclicklistener () {public    voidintlong id) {        //does something in Response to the Click    }};listview.setonitemclicklistener (Mmessageclickedhandler);

Reference Document: Http://developer.android.com/guide/topics/ui/declaring-layout.html

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.