Android布局整合include界面控件

来源:互联网 发布:linux 打包zip 命令 编辑:程序博客网 时间:2024/06/07 02:41

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

由于是讲布局的安排跟组合,那我们这里就只拿布局文件来解析下,其他程序代码跟其他程序没区别。

第一个例子:

这里我们以最简单的控件TextView来举例,总共假设3个布局文件,其中一个布局包含了其他两个子布局。

父布局layoutP:

父布局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代码
  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

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:

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

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

原创粉丝点击