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 View but 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 View instance: , and . The latter, , can be used to create pure XML visual components. (Note: I will present the tag in the next installment of Android Layout Tricks.)

The 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:

            

In the only the layout attribute is required. This attribute, without the android namespace 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:id to 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 tag. 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 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, my next post will explain the , which can be particularly powerful when combined with .

原文出处:

Android Layout Tricks #2: Reusing layouts

更多相关文章

  1. android漂亮EditText效果、背景动画、NFC解析、开发工具、模板项
  2. 【Android(安卓)UI】如何做一个纯粹的Android(安卓)app UI 设计
  3. 网页中android Toast效果的实现
  4. cocos2dx android版本移植时的Error format not a string litera
  5. New Gingerbread API: StrictMode阅读笔记
  6. Android硬件渲染之LockCanvas()
  7. Android(安卓)软件自动更新功能实现的方法
  8. broadcastreceiver 静态注册无法正常运行
  9. Android(安卓)Listview多次调用相同的addHeaderView和addFooterV

随机推荐

  1. android 写 xml时,加layout与不加的区别(如
  2. android:RecyclerView局部刷新那点事~
  3. Android开发工具——ADB(Android(安卓)De
  4. Android热更新实现原理
  5. Android入门学习:Android 系统框架及应用
  6. Android开发100G文档知识大全资料集合
  7. react-native webView android使用本地ht
  8. Android Drawable Resources系列10:
  9. Android跑马灯效果,字体滚动滚屏效果
  10. Android发展史(Android各版本特性-知识篇)