android xml中include标签的使用

1 篇文章 0 订阅

程序员的店http://paperman.taobao.com/



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

现在我们来看一下代码:

    item.xml:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical" >  
    <Button   
        android:id="@+id/btn"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="按钮"/>  
    <EditText   
        android:id="@+id/edt"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="123456"/>  
          
</LinearLayout>  
   item.xml 图示:



 activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical">  
  
    <TextView  
        android:id="@+id/tv"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="@string/hello_world"   
        android:paddingBottom="20dp"/>  
    <include   
        android:id="@+id/my_item"  
        layout="@layout/item"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"/>  
    <include   
        android:id="@+id/my_item2"  
        layout="@layout/item"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"/>  
</LinearLayout>  


 activity_main.xml 图示:



MainActivity.java

package com.example.includedemo;  
  
import android.os.Bundle;  
import android.app.Activity;  
import android.graphics.Color;  
import android.view.Menu;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.LinearLayout;  
import android.widget.TextView;  
  
public class MainActivity extends Activity {  
    private LinearLayout lln1,lln2;  
    private TextView tv;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        tv = (TextView) findViewById(R.id.tv);  
        //初始化xml中id为my_item的控件中的button  
        lln1 = (LinearLayout) findViewById(R.id.my_item);  
        lln1.setBackgroundColor(Color.BLUE);  
        Button btn1 = (Button)lln1. findViewById(R.id.btn);  
          
        //初始化xml中id为my_item2的控件中的button  
        lln2 = (LinearLayout) findViewById(R.id.my_item2);  
        lln2.setBackgroundColor(Color.GREEN);  
        Button btn2 = (Button)lln2. findViewById(R.id.btn);  
          
        btn1.setOnClickListener(new OnClickListener() {  
              
            @Override  
            public void onClick(View v) {  
                tv.setText("第一个 include中的button");  
                  
            }  
        });  
          
        btn2.setOnClickListener(new OnClickListener() {  
              
            @Override  
            public void onClick(View v) {  
                tv.setText("第二个 include中的button");  
                  
            }  
        });  
    }  
  
}  

源码下载链接:  http://download.csdn.net/detail/bx276626237/5516729

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值