需要包含的xml文件,我这里就放了一个Button按钮:

btn.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content"
  5. android:orientation="vertical" >
  6. <Button
  7. android:id="@+id/btn"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="Button">
  11. </Button>
  12. </LinearLayout>

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical"
  6. >
  7. <include android:id="@+id/in1" layout="@layout/btn"/>
  8. <include android:id="@+id/in2" layout="@layout/btn"/>
  9. <TextView android:id="@+id/tv"
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"
  12. android:text="@string/hello" />
  13. </LinearLayout>

TestActivity:

  1. package com.hilary;
  2. import android.app.Activity;
  3. import android.graphics.Color;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.LinearLayout;
  9. import android.widget.TextView;
  10. import com.hialry.R;
  11. /**
  12. *
  13. *@author:hilary
  14. *@Date:2011-12-8
  15. *@description:
  16. *
  17. **/
  18. public class TestActivity extends Activity {
  19. private TextView tv = null;
  20. private LinearLayout ll = null;
  21. private LinearLayout ll2 = null;
  22. /** Called when the activity is first created. */
  23. @Override
  24. public void onCreate(Bundle savedInstanceState) {
  25. super.onCreate(savedInstanceState);
  26. setContentView(R.layout.main);
  27. tv = (TextView) findViewById(R.id.tv);
  28. //如果一个布局文件中包含同一个xml文件,这两个xml中的控件Id是一样的,当需要操作这些控件时,需要通过定义这两个View来加以区分,
  29. //如果就包含同一个xml文件侧不需要此步操作
  30. ll = (LinearLayout) findViewById(R.id.in1);
  31. ll2 = (LinearLayout) findViewById(R.id.in2);
  32. ll.setBackgroundColor(Color.RED);
  33. Button btn = (Button) ll.findViewById(R.id.btn);
  34. btn.setOnClickListener(new OnClickListener() {
  35. @Override
  36. public void onClick(View v) {
  37. tv.setText("My name is hilary");
  38. }
  39. });
  40. Button btn2 = (Button) ll2.findViewById(R.id.btn);
  41. btn2.setOnClickListener(new OnClickListener() {
  42. @Override
  43. public void onClick(View v) {
  44. tv.setText(" You select second Button!");
  45. }
  46. });
  47. }
  48. }

这只是在xml文件中引入另一种布局的一种方法,我们还可以在代码中直接引入,而不需要在xml中定义要引入的文件,在这里就不多说了

from:http://hilary3113.iteye.com/blog/1297416

android include使用[转]的更多相关文章

  1. Android include的使用

    如果在程序中多次用到一部分相同的布局,可以先将这部分布局定义为一个单独的XML,然后在需要的地方通过<include>引入,如下: main.xml <?xml version=&q ...

  2. android include进来的组件 调用其子元素

    include标签包裹着一个可复用的布局: <include layout="@layout/footer_detail" android:id="@+id/foo ...

  3. Android Include标签

    编程的世界有的时候很微妙,有的时候就好像是在解决一个哲学问题,Android开发的时候,所有的布局,颜色,等(其实这些都可以称之为资源,Android中的资源是指非代码部分,如图片.音频.视频.字符等 ...

  4. android include中的控件调用

    项目中经常会有一些布局是重用的,但是如何来更好的利用这些布局中的控件 转: http://zhidao.baidu.com/link?url=GU93U8Wu31dfp7mKEx52hMJkxjFLC ...

  5. Android include和merge标签的使用

    在Android布局文件中,某些时候使用include标签会很多的好处1,对于稍微有点复杂的布局界面,将所有布局代码都写在一个xml文件中,界面会显得很冗余,可读性很差,这时可以分开使用include ...

  6. Android - include属性用法

    include属性用法 本文地址: http://blog.csdn.net/caroline_wendy Android的layout中, 能够使用include属性样式, 这样能够把不同的layo ...

  7. android include标签的使用,在RelativeLayout中使用include标签需注意!!!!!

    转:http://4265337.blog.163.com/blog/static/195375820127935731114/ include和merge标记的作用主要是为了解决layout的重用问 ...

  8. Android在layout xml中使用include

    Android include与merge标签使用详解 - shuqiaoniu的博客 - 博客频道 - CSDN.NEThttp://blog.csdn.net/shuqiaoniu/article ...

  9. 【Android 界面效果25】android中include标签的使用

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

随机推荐

  1. 使用swoole编写简单的echo服务器

    server.php代码如下: <?php class EchoServer { protected $serv = null; public function __construct() { ...

  2. opencv 双边模糊,膨胀腐蚀 开 闭操作

    #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; int main(int argc, ...

  3. Android开发之自定义的ProgressDialog

    package com.example.dialog; import android.app.ProgressDialog; import android.content.Context; /** * ...

  4. NC 5系自定义显示公式

    1.继承NcInnerFunction(nc.vo.pub.formulaset.function.NcInnerFunction) 在方法中引用父类方法function,并在里面写方法 @Overr ...

  5. UI设计工资有多高?怎么快速拿高薪?

    1.UI设计工资有多高? 有人不服UI设计待遇薪资高,那么下面就来看下一线城市的最新UI设计平均薪资待遇,大家也可以打开各招聘网站查询各行业平均薪资情况,一个行业的薪资高不高行业市场决定. 待遇较高说 ...

  6. About how fast is fast enough for a web application?

    About how fast is fast enough for a web application? Here’s a quick overview of key performance metr ...

  7. [RF] Robot Framework新手干货(转载)

    Robot Framework用法总结 Robot Framework完整流程学习系列一 Robotframework自动化新手常见问题总结--(基础篇)

  8. TP QQ 微信 微博登录

    use Org\Util\QQconnect; use Org\Util\Wechatauth; use Org\Util\SaeTOAuthV2; use Org\Util\SaeTClientV2 ...

  9. word2vec_文本相似度

    #提取关键词#关键词向量化#相似度计算 from jieba import analyseimport numpyimport gensim # 实现给出任意字符串,获取字符串中某字符的位置以及出现的 ...

  10. Python : locals and globals

    Python有两个内置的函数,locals() 和globals(),它们提供了基于字典的访问局部和全局变量的方式.Python使用叫做名字空间的东西来记录变量的轨迹.名字空间只是一个 字典,它的键字 ...