android include 控件详解

完整源代码详细解释—聊天、文件传输、语音呼叫,安卓...
http://www.eoeandroid.com/thread-201406-1-1.html

快速查询 IP 电话 物流 火车应用程序
http://www.eoeandroid.com/thread-201970-1-1.html

TabHost内嵌ActivityGroup,界面管理,项目里面很实用,求围观.!
http://www.eoeandroid.com/thread-201401-1-1.html

如何让你的TabHost滑动起来
http://www.eoeandroid.com/thread-202098-1-1.html

android 根据网络地址获取图片的byte[]类型数据
http://www.eoeandroid.com/thread-202079-1-1.html

 

在Android的开发中,我们知道布局文件可以让我们很方便的对各个UI控件进行位置安排跟属性设置,而在程序中可以直接取得控件并赋予对应操作功能。但是,如果是一个复杂的界面设计,我们把所有布局都放在一个文件中来描述,那这个文件会显得比较臃肿而结构则变得无法清晰了。为此,Android为我们提供了一个武功高强的高手,这个高手的特异功能就是能够将几个不同的布局文件整合在一起,它的名字叫include,听名字就知道是包含的意思,当然是包括多个布局。

由于是讲布局的安排跟组合,那我们这里就只拿布局文件来解析下,其他程序代码跟其他程序没区别。
第一个例子:
这里我们以最简单的控件TextView来举例,总共假设3个布局文件,其中一个布局包含了其他两个子布局。
父布局layoutP:

 

<?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">       <include android:id="@+id/cell1" layout="@layout/includeA" />       <include android:id="@+id/cell2"                android:layout_width="fill_parent"                layout="@layout/includeB" />   </LinearLayout>   

 

子布局一layoutA:

<?xml version="1.0" encoding="utf-8"?>   <TextView xmlns:android="http://schemas.android.com/apk/res/android"       android:text="随时随地,即兴时代!"       android:layout_width="wrap_content"       android:layout_height="wrap_content">   </TextView>   

子布局二layoutB:

<?xml version="1.0" encoding="utf-8"?>   <TextView xmlns:android="http://schemas.android.com/apk/res/android"       android:text="ATAAW.COM"       android:layout_width="wrap_content"       android:layout_height="wrap_content">   </TextView>   

通过以上layoutP中的整合,layoutA与layoutB就成为layoutP中的子元素,不仅使得整个布局代码结构清晰,提高了可读性,而且可以将界面排版中的功能模块清楚的划分。

第二个例子:

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

main.xml,

使用include时需要注意的是要指定宽高属性,要不可能会出现一些意想不到的效果,比如引用了三次,而界面上只显示了一个item,需要包含的xml文件,我这里就放了一个Button按钮:
btn.xml:

<?xml version="1.0" encoding="utf-8"?>     <LinearLayout[/b] 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"?> [/align]<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;[/size]     import com.hialry.R;       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() {                @Override        public void onClick(View v) {         tv.setText("My name is hilary");        }       });                          Button btn2 = (Button) ll2.findViewById(R.id.btn);             btn2.setOnClickListener(new OnClickListener() {                @Override        public void onClick(View v) {         tv.setText(" You select second Button!");              }       });         }     }   

 

 

 

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

 

 

你可能感兴趣的:(移动开发,Android开发,include)