android include使用

来源:互联网 发布:前锦网络怎么样上海 编辑:程序博客网 时间:2024/06/14 07:07

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

android include使用

    博客分类: 
  • Android
android

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

 

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

0 0
原创粉丝点击