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

btn.xml:

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

main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"     ><include android:id="@+id/in1" layout="@layout/btn"/><include android:id="@+id/in2" layout="@layout/btn"/>    <TextView android:id="@+id/tv"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/hello" /></LinearLayout>

TestActivity:

package com.hilary;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.LinearLayout;import android.widget.TextView;import com.hialry.R;/****@author:hilary*@Date:2011-12-8*@description:***/public class TestActivity extends Activity {private TextView tv = null;private LinearLayout ll = null;private LinearLayout ll2 = null;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        tv = (TextView) findViewById(R.id.tv);        //如果一个布局文件中包含同一个xml文件,这两个xml中的控件Id是一样的,当需要操作这些控件时,需要通过定义这两个View来加以区分,        //如果就包含同一个xml文件侧不需要此步操作        ll = (LinearLayout) findViewById(R.id.in1);        ll2 = (LinearLayout) findViewById(R.id.in2);                ll.setBackgroundColor(Color.RED);                Button btn = (Button) ll.findViewById(R.id.btn);        btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {tv.setText("My name is hilary");}});                Button btn2 = (Button) ll2.findViewById(R.id.btn);        btn2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {tv.setText(" You select second Button!");}});    }}

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

更多相关文章

  1. Android--用Pull解析器将对象解析成xml文件及单元测试
  2. layout布局属性、标签属性总结大全
  3. android 反射解析xml文件成为java对象
  4. android 9.png 文件
  5. Android TXT文件读写
  6. android java复制和压缩文件或文件夹
  7. Android文件读写简单示例
  8. Android 多线程更新控件
  9. Android又一个超漂亮的日历控件

随机推荐

  1. android中获取wifi信息
  2. Android AlertDialog去除白色边框
  3. Android中创建文件以及文件夹
  4. Android的报错信息at com.android.ddmlib
  5. Android实现广告滚动,ViewFlipper
  6. Android 保存进度对话框
  7. Android 360度摇杆
  8. Android实现记住密码功能
  9. android动态布局:用java代码实现LinearLay
  10. [Android]使用静态变量传递数据