`
158067568
  • 浏览: 327019 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

android布局文件中的include

阅读更多

居然没有发现,在xml文件中可以使用include,我晕

一下为son.xml文件

 

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

 

 接下来在main.xml文件中将其include进来,如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    <include layout="@layout/son"/>
</LinearLayout>
 运行效果不解释了。本来还在想,如果include2次,那么如何区分包含进来的控件的id呢,后来试了一下发现,不管include几次,其实都只相当于include了一次。
分享到:
评论
6 楼 Lee_Dewyze 2011-03-30  
头一次使用了这种做法  布局文件的可重复使用性很强啊
5 楼 wafj1984 2011-02-28  
这个不是很多种布局类的东西都有相似的地方
4 楼 turionboy 2011-02-28  
大惊小怪,
3 楼 c2002509 2011-02-28  
很实用的include
2 楼 wszf 2011-02-27  
共享组件用的
1 楼 zhs2472 2011-02-27  
不错,第一次听说,见识了

相关推荐

    使用include实现布局(layout)复用

    我们是给这些个Activity布局文件都统一加上一样的布局代码,但是维护起来很麻烦,修改不方便,有没有一种类似于编程语言的include语法呢?答案是有的,但是sdk的demo并没有说出使用方法,但这并不说明不好使用,其实...

    Android抽象布局——include、merge 、ViewStub详解

    &lt;include&gt;标签能够重用布局文件,简单的使用如下: &lt;LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:orientation=vertical android:layout_width=”match_parent”

    Android开发之merge结合include优化布局

    布局文件都要有根节点,但android中的布局嵌套过多会造成性能问题,于是在使用include嵌套的时候我们可以使用merge作为根节点,这样可以减少布局嵌套,提高显示速率。 &lt;?xml version=1.0 encoding=utf-8?&gt; ...

    Android中使用include标签和merge标签重复使用布局

    所以当你通过写一个自定义视图创建独立UI组件,你可以放到一个布局文件里,这样更容易复用。 复用布局因为其允许你创建可复用的复杂布局而显得非常强大。如,一个 是/否 按钮面板,或带描述文本的自定义进度条。这...

    Android布局技巧之include、merge与ViewStub标签的巧用

    app开发过程中,会遇到不同页面里有相同的布局,这时我们可以将这些通用的布局提取出来到一个单独的layout文件里,再使用&lt;include&gt;标签引入到相应的页面布局文件里,主要通过include的layout属性引用。  举个栗子...

    Android编程之include文件的使用方法

    我们在android的布局文件中,常会遇到一些相同的布局,每个页面都写,一是比较麻烦,二是一旦有修改还得改多个文件.这个时候我们就可以用到include了. 非常简单的使用,下面看代码 include的文件scollandlisttitle...

    Android开发中include控件用法分析

    当一个activity中的控件非常多的时候,所有的布局文件都放在一个xml文件中,很容易想象那是多么糟糕的事情!笔者通过自身的经历,用include控件来解决这个问题,下面是一个小例子,仅仅实现的是布局,没有响应代码的...

    解析android中include标签的使用

    在一个项目中我们可能会需要用到相同的布局设计,如果都写在一个xml文件中,代码显得很冗余,并且可读性也很差,所以我们可以把相同布局的代码单独写成一个模块,然后用到的时候可以通过&lt;include&gt; 标签来重用layout...

    Android-MenubarLayout:Android布局,可轻松添加菜单栏样式导航

    使用&lt;include&gt;标记添加布局文件,并为其指定一个app:menu_name 。 它们中的每一个都将由MenubarLayout拾取,并显示在菜单选择器中。 ( ) &lt; com .rajasharan.layout.MenubarLayout xmlns : android = " ...

    新版Android开发教程.rar

    In particular, note that some Linux distributions may include JDK 1.4 or Gnu Compiler for Java, both of which are not supported for Android development----------------------------------- Android 编程...

    Android控件CardView实现卡片布局

    CardView是Android 5.0系统引入的控件,相当于FragmentLayout布局控件然后添加圆角及阴影的效果;CardView被包装为一种布局,并且经常在ListView和RecyclerView的Item布局中,作为一种容器使用。CardView应该被使用...

    Android布局技巧之创建可重用的UI组件

    Android平台提供了大量的UI构件,你可以将这些小的视觉块(构件)搭建在一起,呈现给用户复杂且有用的画面。...在Android XML布局文件里,一般,每个标签都对应一个真实的类实例(这些类一般都是View的子类

    Android开发技巧之ViewStub控件惰性装载

    在4.5.6节介绍过一个&lt;include&gt;标签,该标签可以在布局文件中引用另外一个布局文件,并可以覆盖被引用布局文件根节点所有与布局相关的属性,也就是以android:layout开头的属性。通过&lt;include&gt;标签可以将一个非常庞大...

    Android代码-LoadDataLayout

    项目中经常会遇到几种页面:加载中、无网络、无数据、出错四种情况,传统的方式是通过include相关的布局,逐个分情况设置显示或隐藏,这样繁琐的过程一直是个痛点,于是参考了 Weavey 的封装套路,自己重新写了一套...

    Android 组合控件实现布局的复用的方法

    因此,我们要通过自定义View,继承ViewGroup子类来实现这样的布局,降低布局文件和Activity代码耦合性。 首先,我们需要写出布局文件layout_custom_titlebar.xml。 &lt;?xml version=1.0 encoding=utf-8?&gt; &lt;...

    AnotationCodeGenerator-Android:以androidannotation为基础,解析用户布局文件

    基于jdom,读取layout文件(xml)形式,生成符合androidAnnotaion规范的代码。 代码包括: Activity.@EAActivity; Fragment.@Fragment; 类.@EBean; xml解析规则: 解析所有XML节点,id字段赋值以后即命中; 读取的...

    Android系统音量条实例代码

    布局文件是在/frameworks/base/packages/SystemUI/res/layout下。 先看看原生的音量条样式: 在代码中可以发现volume_dialog.xml这个文件,这个文件就是承载音量条的布局了,在layout文件夹找到打开会发现这个布局...

    word源码java-Android-Interview-Gathered:Android-面试-集结

    优化布局文件( Hierarchy Viewer, Layoutopt) 缓存ListView Android多线程的几种方式 Handler.sendXXXMessage() Handler.post(Runnable) Activity.runOnUIThread(Runnable) View.post(Runnable) AsyncTask 布局的...

    Android样式使用

    使用include引用布局文件,使用样式和主题设计导航栏

    Android实现原生侧滑菜单的超简单方式

    首先要写三个xml布局文件,我这里的布局文件是使用了include标签嵌入的,代码如下 headbar_toolbar.xml &lt;?xml version=1.0 encoding=utf-8?&gt; &lt;android.support.v7.widget.Toolbar xmlns:android=...

Global site tag (gtag.js) - Google Analytics