[Android] User interface design

Source: Internet
Author: User

1. Declarative design

There are two ways to design a user interface, procedural and declarative. Procedural design refers to the design of a user interface in code, for example, when developing a swing application, you must write Java code to create and manipulate all user-interface objects. Declarative design does not involve any code, for example, using HTML when designing a Web page, describing the desired page layout rather than how it is implemented.

Android supports both procedural and declarative design, allowing developers to create a user interface in either style, that is, the vast majority of Java code can be used, and most of the XML descriptors can be used. While both are valid, Google recommends using a declarative XML language whenever possible, and XML code tends to be much shorter and easier to understand than the corresponding Java code, and less likely to change in later versions.

The Android program is a loose collection of activities in which each activity defines a user interface screen. R.layout.activity_main is a resource identifier that references the Activity_main.xml file in the Res/layout directory, as follows:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/3A/A6/wKioL1O7_G-AepLAAADtarIwDbU774.jpg "title=" Other-pic.jpg "alt=" Wkiol1o7_g-aeplaaadtariwdbu774.jpg "/>


2. Resource Management

The Activity_main.xml file declares the user interface in XML format, and at run time, Android parses and instantiates the defined resource in the file and sets it as the currently active view. The R class is automatically managed by the Android Eclipse plugin, regardless of where the file is placed in the Res directory, Android The Eclipse plugin will notice this change and automatically add a resource ID within the Gen directory's R.java file, and if you delete or modify a resource file, R.java will remain synchronized as follows:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/3A/A7/wKiom1O7_XTyQoKwAAMf7aYXwVY900.jpg "title=" Other-pic.jpg "alt=" wkiom1o7_xtyqokwaamf7ayxwvy900.jpg "/>android resource Manager uses hexadecimal integers to load actual data, strings, and other resources that are compiled into the package. You do not need to care about the values of these resources, as long as you remember that these values are a handle to the reference data, not a handle to the object that contains the data. Objects are not decompressed before they need to be used.

You can open the file by double-clicking Activity_main.xml in Eclipse, or by double-clicking the layout editor that appears after Activity_main.xml, or an XML editor, depending on the specific settings of Eclipse. Click the Activity_main.xml tab at the bottom to view the XML file, and you can see a reference to <RelativeLayout> as follows:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/3B/4D/wKiom1O9SRTRFC-yAAGj_JzDL2w721.jpg "title=" Other-pic.jpg "alt=" Wkiom1o9srtrfc-yaagj_jzdl2w721.jpg "/>


3. Layout

A layout is a container for one or more sub-objects and behaviors that describe how to place these sub-objects in a rectangular area of their parent on the screen, and the following are some of the most commonly used layouts in Android:

1) Framelayout: Frame layout, starting from the upper left corner of the screen to display sub-objects, mainly used for tab views and image switchers.

2) LinearLayout: A linear layout that displays sub-objects in single-or single-line form, which is the most common layout method.

3) Relativelayout: relative layout, which displays sub-objects relative to the location of other sub-objects or parent objects, is often used in forms.

4) Tablelayout: Table layout, display sub-objects in MultiRow and multicolumn format, similar to HTML tables.

Some of the parameters commonly used in various layout methods are as follows:

1) xmlns:android= "Http://schemans.android.com/apk/res/android" defines the XML namespace for Android, which can only be defined once at the first XML tag in the Main.xml file.

2) android:layout_width= "Fill_parent", android:layout_height= "Fill_parent", declares that the height and width of this layout are the same as the height and width of the parent object.

If you see a warning in the editor about missing syntax constraints, you can refer to the string in the Res/values/strings.xml file by using @string/resid as a syntax that is different from hard-coded English text into a layout file, although it is ignored. You can also have additional resource files, depending on the locale or other parameter settings. You can also use a new syntax form: @id/resid, unlike resource IDs defined elsewhere, this syntax creates a new resource ID for other object references.


4. Implement the dialog window

You can create a pop-up prompt window on the screen and display some information about the window, and after reading the text, you can press the button to close the window. There are several ways to implement this functionality:

1) Define a new activity class and start it.

2) Use the Alertdialog class and display it.

3) Subclass the Android dialog class and display it.

The following defines a new activity, which also requires a layout file, such as naming the file Res/layout/activity_about.xml, as follows:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/40/54/wKiom1POZubBEJYxAAMZ_NvAS5c304.jpg "title=" Other-pic.jpg "alt=" Wkiom1pozubbejyxaamz_nvas5c304.jpg "/>

The about activity should be defined in the Aboutactivity.java file, simply overwrite the OnCreate () method and call the Setcontentview () method, as follows:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/3D/BF/wKiom1PFMCuToX_hAAFoqwgolE4522.jpg "title=" Other-pic.jpg "alt=" wkiom1pfmcutox_haafoqwgole4522.jpg "/> Add a button to the Activity_main.xml to associate with the about activity as follows:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/3D/BF/wKioL1PFMMCwd7a1AAKFa4Rsxh8414.jpg "title=" Other-pic.jpg "alt=" Wkiol1pfmmcwd7a1aakfa4rsxh8414.jpg "/> and then add some import statements in Mainactivity.java, in OnCreate () method, add code to call the Findviewbyid () and Setonclicklistener () methods, and the Findviewbyid () method is used to find the Android view based on the resource ID, Setonclicklistener () Method tells Android which object should be triggered when the user touches or clicks the view, as follows:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/3D/C0/wKiom1PFNFby-lN9AAKzy6dGorA501.jpg "title=" Other-pic.jpg "alt=" Wkiom1pfnfby-ln9aakzy6dgora501.jpg "/> To start an activity in Android, you first need to create an instance of the intent class. There are two kinds of intent,public in Android intent are registered at the system level, can be used from any application, private intent can only be used within one application. In addition, each activity needs to be declared in the Androidmanifest.xml file, which works as follows:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/3D/C5/wKiom1PFQziQwnW4AAB14r8gx7Y751.jpg "title=" Other-pic.jpg "width=" "height=" 417 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:250PX;HEIGHT:417PX; "alt=" Wkiom1pfqziqwnw4aab14r8gx7y751.jpg "/>


5. Apply Themes and menus

A theme is a collection of styles that can be used to override the appearance of an Android part, with multiple themes attached to Android that can be referenced by name. Also, users can construct their own themes by subclasses of existing topics and overwrite their default values. You can customize the theme in the Res/values/styles.xml file, to use the theme, first open the Androidmanifest.xml file again in the editor, and then modify the definition of the about activity as follows:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/3F/A7/wKioL1PLyFDQO33JAABuYacaGys492.jpg "title=" Other-pic.jpg "alt=" wkiol1plyfdqo33jaabuyacagys492.jpg "/> style name before the @android: prefix indicates that the resources defined by Android are referenced here, Instead of the user's own resources defined in the program, run the program again as follows:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/3F/A7/wKioL1PLyxnhqz20AACvrGQEvj8874.jpg "title=" Other-pic.jpg "width=" "height=" 417 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:250PX;HEIGHT:417PX; "alt=" Wkiol1plyxnhqz20aacvrgqevj8874.jpg "/>

Android supports two types of menus: a menu that pops up when the user presses the menu button, and a context menu that pops up when the user holds the screen down with their finger. Taking the first vegetable list as an example, you need to use the menu defined in the Res/menu/main.xml file as follows:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/40/54/wKiom1POZl6i3GK-AAC8BCUBppc970.jpg "title=" Other-pic.jpg "alt=" Wkiom1pozl6i3gk-aac8bcubppc970.jpg "/>

Then use the Oncreateoptionsmenu () method in the Mainactivity class as follows:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/40/53/wKioL1POZ0jgCzhZAAI0o76K1cU580.jpg "title=" Other-pic.jpg "alt=" Wkiol1poz0jgczhzaai0o76k1cu580.jpg "/>

The Getmenuinflater () method returns an Menuinflater instance that reads the menu definition from the XML file and converts it to the actual view, and the onoptionsitemselected () method is called when the user selects any menu item.



This article from "Fangyuan" blog, declined reprint!

[Android] User interface design

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.