Android Layout Trick #2: Include to Reuse
2016-05-21 13:21
561 查看
Android comes with a wide variety of widgets, small visual construction blocks you can glue together to present the users with complex and
useful interfaces. However applications often need higher level visual components. A component can be seen as a complex widget made of
several simple stock widgets. You could for instance reuse a panel containing a progress bar and a cancel button, a panel containing two buttons (positive and negative actions), a panel with an icon, a title and
a description, etc. Creating new components can be done easily by writing a custom
it can be done even more easily using only XML.
In Android XML layout files, each tag is mapped to an actual class instance (the class is always a subclass of View.)
The UI toolkit lets you also use three special tags that are not mapped to a
(Note: I will present the
Layout Tricks.)
The
tag is straightforward as shown in the following example, taken straight from the
source code of the Home application that currently ships with Android:
In the
is required. This attribute, without the
prefix, is a reference to the layout file you wish to include. In this example, the same layout is included three times in a row.
This tag also lets you override a few attributes of the included layout.
The above example shows that you can use
specify the id of the root view of the included layout; it will also override the id of the included layout if one is defined. Similarly, you can override all the layout parameters. This means that any
can be used with the
Here is an example:
This tag is particularly useful when you need to customize only part of your UI depending on the device’s configuration.
For instance, the main layout of your activity can be placed in the
and can include another layout which exists in two flavors, in
This allows you to share most of the UI in portrait and landscape.
Like I mentioned earlier, the next installment will explain the
原文URL: http://www.curious-creature.com/2009/02/25/android-layout-trick-2-include-to-reuse/
useful interfaces. However applications often need higher level visual components. A component can be seen as a complex widget made of
several simple stock widgets. You could for instance reuse a panel containing a progress bar and a cancel button, a panel containing two buttons (positive and negative actions), a panel with an icon, a title and
a description, etc. Creating new components can be done easily by writing a custom
Viewbut
it can be done even more easily using only XML.
In Android XML layout files, each tag is mapped to an actual class instance (the class is always a subclass of View.)
The UI toolkit lets you also use three special tags that are not mapped to a
Viewinstance:
<requestFocus />,
<merge />and
<include />. The latter,
<include />, can be used to create pure XML visual components.
(Note: I will present the
<merge />tag in the next installment of Android
Layout Tricks.)
The
<include />does exactly what its name suggests; it includes another XML layout. Using this
tag is straightforward as shown in the following example, taken straight from the
source code of the Home application that currently ships with Android:
<com.android.launcher.Workspace android:id="@+id/workspace" android:layout_width="fill_parent" android:layout_height="fill_parent" launcher:defaultScreen="1"> <include android:id="@+id/cell1" layout="@layout/workspace_screen" /> <include android:id="@+id/cell2" layout="@layout/workspace_screen" /> <include android:id="@+id/cell3" layout="@layout/workspace_screen" /> </com.android.launcher.Workspace>
In the
<include />only the
layoutattribute
is required. This attribute, without the
androidnamespace
prefix, is a reference to the layout file you wish to include. In this example, the same layout is included three times in a row.
This tag also lets you override a few attributes of the included layout.
The above example shows that you can use
android:idto
specify the id of the root view of the included layout; it will also override the id of the included layout if one is defined. Similarly, you can override all the layout parameters. This means that any
android:layout_*attribute
can be used with the
<include />tag.
Here is an example:
<include android:layout_width="fill_parent" layout="@layout/image_holder" /> <include android:layout_width="256dip" layout="@layout/image_holder" />
This tag is particularly useful when you need to customize only part of your UI depending on the device’s configuration.
For instance, the main layout of your activity can be placed in the
layout/directory
and can include another layout which exists in two flavors, in
layout-land/and
layout-port/.
This allows you to share most of the UI in portrait and landscape.
Like I mentioned earlier, the next installment will explain the
<merge />, which can be particularly powerful when combined with
<include />.
原文URL: http://www.curious-creature.com/2009/02/25/android-layout-trick-2-include-to-reuse/
相关文章推荐
- 编译带有OpenCL的OpenCVForAndroid
- Android学习系列(1)--------apk签名
- 使用新版Eclips编写的APK(V 7包)放入Android源码中mmm编译,报错的两种解决办法
- Android6.0 PROTECTION_NORMAL类总结
- Android Layout Tricks #3: Optimize, Part 1
- Android递归遍历文件夹中指定格式文件
- 解决Android Studio源码的乱码问题
- android启动模式简介
- Android开发实践 Intent 解析
- 自定义View之继承TextView绘制背景
- Android中解析XML
- Android使用Font Awesome显示小图标
- Android新特性之一
- Android之LayoutInflater探索
- 如何理解android中的Context?
- Android 开源项目PhotoView遇到问题
- Android 对data/data/(your packagename)目录下的数据读写、删除操作
- Android控件之DatePicker、TimePicker探究
- 【Android】引用布局
- Android day3:Java浮点类型、转义字符、布尔类型