android xml中使用include标签

在一个项目中,我们可能会在xml中局部用到相同的布局,如果每次都在xml中重写这些布局,代码显得很冗余、重复的复制黏贴也很烦恼,所以,我们把这些相同的局部布局写成一个单独的xml模块,需要用到这些布局时,在要使用的xml中引入这些布局,而引用布局时所需使用的头标签就是<include />。

 

现在我们来看一下代码:

    item.xml:

[html]   view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:layout_width="match_parent"    
  4.     android:layout_height="match_parent"    
  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="按钮"/>    
  11.     <EditText     
  12.         android:id="@+id/edt"    
  13.         android:layout_width="wrap_content"    
  14.         android:layout_height="wrap_content"    
  15.         android:text="123456"/>    
  16.             
  17. </LinearLayout>    

   item.xml 图示:


 activity_main.xml:

 

[html]   view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  2.     xmlns:tools="http://schemas.android.com/tools"    
  3.     android:layout_width="match_parent"    
  4.     android:layout_height="match_parent"    
  5.     android:orientation="vertical">    
  6.     
  7.     <TextView    
  8.         android:id="@+id/tv"    
  9.         android:layout_width="wrap_content"    
  10.         android:layout_height="wrap_content"    
  11.         android:text="@string/hello_world"     
  12.         android:paddingBottom="20dp"/>    
  13.     <include     
  14.         android:id="@+id/my_item"    
  15.         layout="@layout/item"    
  16.         android:layout_width="wrap_content"    
  17.         android:layout_height="wrap_content"/>    
  18.     <include     
  19.         android:id="@+id/my_item2"    
  20.         layout="@layout/item"    
  21.         android:layout_width="wrap_content"    
  22.         android:layout_height="wrap_content"/>    
  23. </LinearLayout>    

 

 

 activity_main.xml 图示:


MainActivity.java

 

[java]   view plain copy
  1. package com.example.includedemo;    
  2.     
  3. import android.os.Bundle;    
  4. import android.app.Activity;    
  5. import android.graphics.Color;    
  6. import android.view.Menu;    
  7. import android.view.View;    
  8. import android.view.View.OnClickListener;    
  9. import android.widget.Button;    
  10. import android.widget.LinearLayout;    
  11. import android.widget.TextView;    
  12.     
  13. public class MainActivity extends Activity {    
  14.     private LinearLayout lln1,lln2;    
  15.     private TextView tv;    
  16.     @Override    
  17.     protected void onCreate(Bundle savedInstanceState) {    
  18.         super.onCreate(savedInstanceState);    
  19.         setContentView(R.layout.activity_main);    
  20.         tv = (TextView) findViewById(R.id.tv);    
  21.         //初始化xml中id为my_item的控件中的button    
  22.         lln1 = (LinearLayout) findViewById(R.id.my_item);    
  23.         lln1.setBackgroundColor(Color.BLUE);    
  24.         Button btn1 = (Button)lln1. findViewById(R.id.btn);    
  25.             
  26.         //初始化xml中id为my_item2的控件中的button    
  27.         lln2 = (LinearLayout) findViewById(R.id.my_item2);    
  28.         lln2.setBackgroundColor(Color.GREEN);    
  29.         Button btn2 = (Button)lln2. findViewById(R.id.btn);    
  30.             
  31.         btn1.setOnClickListener(new OnClickListener() {    
  32.                 
  33.             @Override    
  34.             public void onClick(View v) {    
  35.                 tv.setText("第一个 include中的button");    
  36.                     
  37.             }    
  38.         });    
  39.             
  40.         btn2.setOnClickListener(new OnClickListener() {    
  41.                 
  42.             @Override    
  43.             public void onClick(View v) {    
  44.                 tv.setText("第二个 include中的button");    
  45.                     
  46.             }    
  47.         });    
  48.     }    
  49.     
  50. }    

转载于:https://www.cnblogs.com/Cherry-B/p/3338147.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值