读书笔记[Android Beginning 2] chapter 4 Using XML-Based Layouts

来源:互联网 发布:qq自动加人软件 编辑:程序博客网 时间:2024/05/16 11:49

Dynamic instantiation of widgets is reserved for more complicated scenarios, where the widgets are not known at compile time (e.g.,populating a column of radio buttons based on data retrieved from the Internet).


What Is an XML-Based Layout?

an XML-based layout is a specification of widgets’ relationships to each other—and to their containers —encoded in XML format.

Specifically, Android considers XML-based layouts to be resources, and as such, layout files are stored in the res/layout directory inside your Android project.

Each XML file contains a tree of elements specifying a layout of widgets and containers that make up one View hierarchy. 

The attributes of the XML elements are properties, describing how a widget should look or how a container should behave. 

Android’s SDK ships with a tool (aapt) that uses the layouts. This tool should be automatically invoked by your Android toolchain. 

aapt generates the R.java source file within your project, allowing you to access layouts and widgets within those layouts directly from your Java code.


Why Use XML-Based Layouts?

Most everything you do using XML layout files can be achieved through Java code.

Android GUI designer : DroidDraw.

Microsoft’s Extensible Application Markup Language (XAML), Adobe’s Flex, and Mozilla’s XML User Interface Language (XUL) all take a similar approach to that of Android: put layout details in an XML file and put programming smarts in source files (e.g., JavaScript for XUL). Many less well-known GUI frameworks, such as ZK, also use XML for view definition.


OK, So What Does It Look Like?

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button"
android:text=""
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>

The class name of the widget, Button, forms the name of the XML element. Since Button is an Android-supplied widget, we can just use the bare class name. If you create your own widgets as subclasses of android.view.View, you will need to provide a full package declaration as well (e.g., com.commonsware.android.MyWidget).

The root element needs to declare the Android XML namespace:
xmlns:android="http://schemas.android.com/apk/res/android"
All other elements will be children of the root and will inherit that namespace declaration.

Because we want to reference this button from our Java code, we need to give it an identifier via the android:id attribute.


What’s with the @ Signs?

Many widgets and containers need to appear only in the XML layout file and do not need to be referenced in your Java code. These sorts of elements in the XML file do not need to have the android:id attribute to give them a name.

Anything you do want to use in your Java source, though, needs an android:id. The convention is to use @+id/... as the id value, where the ... represents your locally unique name for the widget in question.

Android provides a few special android:id values, of the form @android:id/...


And How Do We Attach These to the Java?

all you need is one statement in your activity’s onCreate() callback to use that layout: setContentView(R.layout.main);

All of the layouts are accessible under R.layout, keyed by the base name of the layout file; for example, res/layout/main.xml results in R.layout.main.

To access your identified widgets, use findViewById(), passing in the numeric identifier of the widget in question. That numeric identifier was generated by Android in the R class as R.id.something (where something is the specific widget you are seeking). Those widgets are simply subclasses of View.


原创粉丝点击