欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Android布局xml的include

在一個項目中我們可能會需要用到相同的布局設(shè)計,如果都寫在一個xml文件中,代碼顯得很冗余,并且可讀性也很差,所以我們可以把相同布局的代碼單獨寫成一個模塊,然后用到的時候可以通過<include /> 標簽來重用layout代碼。

網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、成都微信小程序、集團企業(yè)網(wǎng)站建設(shè)等服務項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了南山免費建站歡迎大家使用!




btn.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

  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:

  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來加以區(qū)分,  

  30.         //如果就包含同一個xml文件側(cè)不需要此步操作  

  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文件中,界面會顯得很冗余,可讀性很差。而且界面加載的時候是按照順序加載的,前面的布局不能調(diào)用其后面的布局id。而采用include后,一個include中可以引用其后的include中的布局id屬性 -->  

當前文章:Android布局xml的include
當前URL:http://www.chinadenli.net/article28/iphccp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)網(wǎng)站營銷手機網(wǎng)站建設(shè)面包屑導航靜態(tài)網(wǎng)站微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司