读书笔记[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.
- 读书笔记[Android Beginning 2] chapter 4 Using XML-Based Layouts
- Using XML-Based Layouts 笔记
- 读书笔记[Android Beginning 2] chapter 1
- 读书笔记[Android Beginning 2] chapter 2 Create a Skeleton Application
- 读书笔记[Android Beginning 2] chapter 5 Employing Basic Widgets
- 读书笔记 - Beginning Linux Programming - Chapter 2 Shell Programming
- Chapter 11: Using Menu-Based Controls--About menu-based controls
- Chapter 11: Using Menu-Based Controls--Menu-based control events
- Beginning Linux Programming chapter 4
- Android 开发指南 翻译2 User Interface:XML Layouts
- Android XML布局详解 - XML Layouts
- 读书笔记 - Beginning Linux Programming - Chapter 1 Getting Started
- Chapter 11: Using Menu-Based Controls--Menu control
- Chapter 11: Using Menu-Based Controls--MenuBar control
- Chapter 11: Using Menu-Based Controls--PopUpMenuButton control
- XML Layouts
- beginning python summary chapter 4 - 字典
- perfbook读书笔记 chapter 2
- Oracle中十进制与十六进制转换程序
- WM_DRAWITEM与DrawItem()的讨论
- SQL 语言支持
- WCF概要
- 精品之路
- 读书笔记[Android Beginning 2] chapter 4 Using XML-Based Layouts
- 1011 诗一样的石子
- ubuntu man手册完善
- 1013 喝醉的选手
- Android 读取资源文件
- 1014 碰撞的机器人
- Android日志系统驱动程序Logger源代码分析
- 1015 约瑟夫
- 1016 超级病毒