分享

android include 控件详解

 杰出天下 2012-10-24

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

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

 

复制代码
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas./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./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./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./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./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中定义要引入的文件.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约