资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Android布局xml的include

在一个项目中我们可能会需要用到相同的布局设计,如果都写在一个xml文件中,代码显得很冗余,并且可读性也很差,所以我们可以把相同布局的代码单独写成一个模块,然后用到的时候可以通过 标签来重用layout代码。

网站建设哪家好,找成都创新互联公司!专注于网页设计、网站建设、微信开发、成都微信小程序、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了南山免费建站欢迎大家使用!




btn.xml:

  1.   

  2.     android:layout_width="fill_parent"  

  3.     android:layout_height="wrap_content"  

  4.     android:orientation="vertical" >  

  5.    

  6.         android:id="@+id/btn"  

  7.         android:layout_width="wrap_content"  

  8.         android:layout_height="wrap_content"   

  9.         android:text="Button">  

  10.       

  11.   

复制代码

main.xml

  1.   

  2.     android:layout_width="fill_parent"  

  3.     android:layout_height="fill_parent"  

  4.     android:orientation="vertical"   

  5.     >  

  6.       

  7.       

  8.    

  9.         android:layout_width="fill_parent"  

  10.         android:layout_height="wrap_content"  

  11.         android:text="@string/hello" />  

  12.   

复制代码

TestActivity:

  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. *@author:hilary 

  15. *@Date:2011-12-8 

  16. *@description: 

  17. **/  

  18. public class TestActivity extends Activity {  

  19.     private TextView tv = null;  

  20.     private LinearLayout ll = null;  

  21.     private LinearLayout ll2 = null;  

  22.       

  23.     /** Called when the activity is first created. */  

  24.     @Override  

  25.     public void onCreate(Bundle savedInstanceState) {  

  26.         super.onCreate(savedInstanceState);  

  27.         setContentView(R.layout.main);  

  28.         tv = (TextView) findViewById(R.id.tv);  

  29.         //如果一个布局文件中包含同一个xml文件,这两个xml中的控件Id是一样的,当需要操作这些控件时,需要通过定义这两个View来加以区分,  

  30.         //如果就包含同一个xml文件侧不需要此步操作  

  31.         ll = (LinearLayout) findViewById(R.id.in1);  

  32.         ll2 = (LinearLayout) findViewById(R.id.in2);  

  33.           

  34.         ll.setBackgroundColor(Color.RED);  

  35.           

  36.         Button btn = (Button) ll.findViewById(R.id.btn);  

  37.         btn.setOnClickListener(new OnClickListener() {  

  38.               

  39.             @Override  

  40.             public void onClick(View v) {  

  41.                 tv.setText("My name is hilary");  

  42.             }  

  43.         });  

  44.           

  45.         Button btn2 = (Button) ll2.findViewById(R.id.btn);  

  46.         btn2.setOnClickListener(new OnClickListener() {  

  47.               

  48.             @Override  

  49.             public void onClick(View v) {  

  50.                 tv.setText(" You select second Button!");  

  51.                   

  52.             }  

  53.         });  

  54.     }  

  55. }  

复制代码

如果没有include标签,所有布局代码都写在一个xml文件中,界面会显得很冗余,可读性很差。而且界面加载的时候是按照顺序加载的,前面的布局不能调用其后面的布局id。而采用include后,一个include中可以引用其后的include中的布局id属性 -->  


本文标题:Android布局xml的include
本文网址:http://cdkjz.cn/article/iphccp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220