Android SDK Get Started Guide 2: User interface design

Source: Internet
Author: User
Tags closing tag list of attributes home screen

Http://mobile.51cto.com/ahot-419184.htm

Content Introduction

We'll add a layout scheme to the application project, in which the XML and Eclipse ADT interfaces will be the right helpers for the job-but some Java development knowledge will be used in the next two sections. XML and Java in the development of the Android platform is everywhere, if you do not have a basic understanding of the two, please find ways to fill up the lessons as soon as possible. For those who are just getting started, the key points presented in this article will be an important basis for future development work.

1. XML Basics

Before we start discussing the layout, let's comb the basics of XML as a markup language. If you are already familiar with XML, you can skip this section directly. XML is a language used to hold data values. XML files play a role in many areas. They are functionally similar to databases in some projects and are often used as the output mechanism of web pages. If you've ever used HTML before, you should be familiar with the basics of XML.

In XML, the data values are stored in the element. A single element typically contains a start tag with a closing tag, as follows:

    1. <product>Onion</product>

As you can see, the start tag is almost exactly the same as the closing tag, and the only difference is that there is one more "/" sign in the closing tag. In the above example, the data value is the element content, which is the text string "Onion". The start tag can also hold additional attribute information that is believed with the data item, as follows:

    1. <product type= "Vegetable" >Onion</product>

Each property has a name and a value, where the value is the part within the quotation marks. Elements can also contain other elements:

    1. <section name= "Food" ><font></font>
    2. <product type="vegetable">Onion</product><font></font>
    3. <product type="fruit">Banana</product><font></font>
    4. </section><font></font>

In this structure, we call the section element the main element, the products element is called the child element. Two child elements belong to a "brotherhood". In an XML document, there must be a root element as the primary element, or "nested". This constitutes a tree structure in which the child elements are branched out as autonomous elements. If a child element contains other child elements, it also has the main element attribute.

You will also encounter another self-closing element where the start and end tags do not exist independently:

    1. <order number= "12345" customerid= "a4d45s" />

The "/" symbol at the end of the element represents the end.

All of the resource files we use on the Android platform are XML tags, including layout files, drawing elements, data values, and manifest.

2. Android layout

The first step

When you use XML in the Eclipse IDE where ADT is installed, the relevant background hints displayed during the input process make the encoding process easier. Open the main layout file in the new app in the editor and make sure that the XML edit tag is selected so that we can edit the code directly. The first thing we want to deal with is the layout scheme for the home screen, which is what the user sees first after launching the app. Eclipse provides a set of basic layouts that we can personalize to modify:

  1. <relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android" <font></font>
  2. xmlns:tools="Http://schemas.android.com/tools"<font></font>
  3. Android:layout_width="match_parent"<font></font>
  4. android:layout_height="match_parent"<font></font>
  5. android:paddingbottom="@dimen/activity_vertical_margin"<font></font>
  6. android:paddingleft="@dimen/activity_horizontal_margin"<font></font>
  7. android:paddingright="@dimen/activity_horizontal_margin"<font></font>
  8. android:paddingtop="@dimen/activity_vertical_margin"<font></font>
  9. Tools:context=". Mainactivity " ><font></font>
  10. <TextView<font></font>
  11. Android:layout_width="wrap_content"<font></font>
  12. android:layout_height="wrap_content"<font></font>
  13. android:text="@string/hello_world" /><font></font>
  14. </RelativeLayout><font></font>

As you can see, the root element is a layout element, which in the example above is relativelayout. There are several other layout types available in Android, and we can nest one layout into another. The root layout element here has several additional properties that are closely related to the layout effect, such as width, height, margins, and so on. The TextView in the layout element allows the developer to display a text string. TextView is a view, and view is a visible and interactive element that forms our application UI. Therefore, each set of split-screen scenarios in an application chooses a view and contains one or more layout mechanisms. In Android, these layouts are called ViewGroup objects, and each viewgroup contains one or more sets of view.

Step Two

To focus on the basics of a set of layouts, we want to remove all the existing content from the main layout file so that we can design from scratch. As we mentioned earlier, you can use Java code to create your own layouts or view, but many of the tools on Android allow developers to design their own applications using XML ui--so that you can see the design effect directly while creating elements. In some instances, you may have seen a simple way to create some or all of the UI through Java code, but in reality most of the creation work is done by XML. This approach also ensures that application logic and display elements are independent of each other.

    1. <linearlayout xmlns:android= " Http://schemas.android.com/apk/res/android " <font></font> 
    2.     android:layout_width= "fill_parent" <font> </font> 
    3.     android:layout_height=< Span class= "string" > "fill_parent" <font></font> 
    4.     android:orientation= "vertical"  ><font> </font> 
    5.     <!-- views go  here --><font></font> 
    6. </LinearLayout><font> </font> 

LinearLayout will display the view we intend to use along the landscape or portrait. In the example above, the orientation is vertical, so each view is arranged along the bottom of the screen. If you take a horizontal layout, the individual view is arranged from left to right. If you use both the layout width and layout height properties (which are often referred to as layout parameters in Android), the layout is stretched to the maximum horizontal and vertical lengths.

Add a new line after the "layout height" declaration line and start typing the property by typing "android:". When you enter the corresponding content, eclipse will provide a set of lists related to that attribute. You can continue to enter content to narrow the list of attributes, or you can click the mouse button directly in the list. Now we select the "Android:gravity" property.

Type "Center_horizontal" as the gravity value, so that the elements contained in it are displayed in the center of the x-axis:

    1. android:gravity = "Center_horizontal"  

This approach applies to all elements in the layout. We can add several additional display properties, such as padding, margins, and backgrounds. But in today's article, let's start with the simplest project.

3. Add View

The first step

Front we start adding view to the layout. The so-called view refers to the visible elements in the UI. Let's start by adding some text content and a button. Enter the LinearLayout element (between the start 忹 end tag), and after entering "<", Eclipse will prompt you for a list of available elements related to the attribute.

Select TextView in the list. Note that, like most view, this is a self-closing element. Set two properties for TextView, respectively layout width and layout height (type ' android: ' and select the corresponding hint):

    1. <TextView<font></font>
    2. Android:layout_width="wrap_content"<font></font>
    3. android:layout_height="wrap_content" /><font></font>

With "Wrap_content", we can guarantee that the view is wide enough to hold its display-this avoids the display of elements in a fill-like manner as a layout. Now add another property for TextView, this time by enumerating the text strings to implement the display function:

    1. Android:text = "Hello There"  

After saving the file, you will see that eclipse displays a warning message. If you hover over a message, the text will appear at the border of the editor--and it will also appear in the problem view. The warning content is "hardcoded string......should use @string resource (hard-coded string ...). Should use @string resources). "It is recommended that you save each text string value as a value resource instead of being included directly in the layout XML. Although this approach is cumbersome and meaningless from the start, once you develop a good habit, you will gradually discover its value in large projects in future work. Find the "res/values/strings.xml" file and open it through the Package Explorer, switch to the "Strings.xml" tab and edit the code.

As you can see, Eclipse has added several strings. To add it separately, simply set the name and value for it:

    1. <string name= "Hello" >hello there</string>

This means that if you need to use the same string more than once in the application UI and later need to modify it, you can only make changes in one place. Save the string file and switch to the layout file. Reference the TextView "text" property to the corresponding string in the value file:

    1. android:text= "@string/hello"  

We tell the Android tool where to look for string resources by adding "@string" to the string name. As a result, the warning message will no longer appear. Eclipse usually sends these reminders during our coding process to notify us of any current errors or warning issues. You can choose to follow or ignore the contents of the warning message, but the error must be adjusted, or the application will not work correctly.

Step Two

Add a button after TextView:

    1. <Button<font></font>
    2. Android:layout_width="wrap_content"<font></font>
    3. android:layout_height="wrap_content"<font></font>
    4. android:text="@string/click" /><font></font>

In our example, the button uses the same properties as TextView. In other cases, however, it may use more properties, and generally different views need to match different attributes. The "text" property value is displayed on the button. Add this string to our "Res/values/strings.xml" file as before:

    1. <string name= "click" >click me!</string>

In the next tutorial, we will handle the click effect of the button. Switch to the layout file to see the outline view on the right side of the editor-it shows another set of interfaces that point to the file element. Double-click the listed item to jump to the corresponding code location. You can also expand or collapse the main element. This approach becomes very useful when the layout becomes more complex.

Tip: To organize all the files that are open in eclipse editing, we simply press "CTRL + a" to select them all, then press "Ctrl+i".

4. Graphical Layout

The first step

Make sure that our layout files are saved correctly, and then switch to the graphical Layout tab.

You can see that the layout you have designed has been able to be viewed directly. The palette area on the left side of the interface allows us to select a UI item and drag it to the layout. However, we should first use XML until we have a preliminary idea of the basic framework. XML can help us control the detail design, so even when using graphical tools, we may need to edit the XML results.

Above the graphical layout view is a set of drop-down lists from which we can select the type of device used to view the layout effect, which also provides tools to toggle the display orientation and zoom effect. We need to use the graphical layout to control the effect in the process of designing layout. In addition, there are some other layout elements and settings that are worth trying.

Step Two

You may have noticed that in this one layout design, the visible elements are displayed in close proximity to the top edge of the screen. Here's how to solve this problem. Switch to the XML Edit tab and add the margin attribute to LinearLayout:

    1. android:layout_margin= "10DP"  

We use "DP" to set the independent density of pixels, so that the design will automatically match the pixel density to the user's device. Save the file and switch to graphical layout to see the actual effect.

In our layout design, graphical layout is a very useful reference tool, but can only play a guiding effect. To understand how our layouts display and function when the application is running, you need to load them into virtual or physical devices for practical difficulty. We will discuss this topic further in a follow-up article.

5. Options

You can include various types of layouts and view in the application screen, but they are basically handled in a consistent way. We used to use the linearlayout, but there are a number of other options available, including Relativelayout, Framelayout, Absolutelayout, and GridLayout, which are more common. You can find these types in the LinearLayout palette, and suggest that you relax your mind, choose any of your own view and see how it looks. When adding elements from the graphical layout tool, be sure to switch to XML to see what markup code will be generated by the addition of the new element.

The Android platform provides view solutions for a variety of common needs, such as radio buttons, checkboxes, and text input areas. These programs can greatly reduce the number of functions we need to perform manually, but if you need to use non-self-bringing UI elements, you need to create a custom view class. In general, it is better to do so when there is no other option, after all, the standardized UI elements are more reliable on the user's device and can save time for development and testing.

Conclusion

In today's tutorial, we discussed the basic design flow of the user interface layout on the Android platform, but did not dig deep. In the next section of this series of articles, we will try to add user interaction elements, detect and respond to button clicks in the application. Next, we'll look at the Java-related concepts closest to Android development, and further explore the elements and practices involved in the application development process.

Original link:

http://mobile.tutsplus.com/tutorials/android/android-sdk-user-interface-design/

Original title: Android sdk:user Interface Design

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.