天天看點

android xml中include标簽的使用

程式員的店:http://paperman.taobao.com/

android xml中include标簽的使用

在一個項目中,我們可能會在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 圖示:

android xml中include标簽的使用

 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 圖示:

android xml中include标簽的使用

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