歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android中include標簽的使用

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

app_title.xml:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout Android:id="@+id/titleLayout" android:layout_width="fill_parent" android:layout_height="wrap_content"  
  3.             android:background="@drawable/bt" xmlns:android="http://schemas.android.com/apk/res/android">  
  4.               
  5.         <TextView android:text="@string/login" android:id="@+id/title" android:textSize="20px" android:textColor="@color/white"  
  6.             android:layout_width="wrap_content" android:layout_centerInParent="true" android:layout_height="wrap_content"/>        
  7.         <Button android:text="返回" android:id="@+id/refresh" android:layout_width="wrap_content" android:layout_height="wrap_content"  
  8.             android:layout_alignParentRight="true" android:focusable="false" android:textColor="@color/white"  
  9.             android:background="@drawable/okbutton" android:layout_marginRight="3px"/>  
  10. </RelativeLayout>  

app_tradelogin.xml:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <!-- android:layout_below與 android:layout_above 是必須設置的,否則界面不規則,不設置android:layout_above本界面沒有滑動效果-->  
  4.   
  5. <ScrollView android:layout_below="@+id/titleLayout" android:layout_above="@+id/appbottom" android:layout_width="fill_parent"   
  6.      android:layout_height="fill_parent" android:layout_centerInParent="true" xmlns:android="http://schemas.android.com/apk/res/android">  
  7.   
  8.     <LinearLayout android:id="@+id/theWholeLinearLayout" android:orientation="vertical" android:layout_width="fill_parent"   
  9.         android:layout_height="wrap_content" android:paddingLeft="10px" android:paddingTop="6px" android:paddingRight="10px" >  
  10.         <LinearLayout android:id="@+id/linearLayoutLeft" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content">  
  11.             <TableLayout android:id="@+id/widget43" android:layout_width="fill_parent"  
  12.                 android:layout_height="fill_parent" android:orientation="vertical"  
  13.                 xmlns:android="http://schemas.android.com/apk/res/android" android:stretchColumns="1">  
  14.                 <TableRow >   
  15.                 <!--  引用定義好的TextView樣式,如果這裡的屬性和樣式裡定義的屬性重復,則這裡會替換樣式裡定義的屬性-->  
  16.                     <TextView android:id="@+id/traderName" android:text="@string/traderName"  mce_/>  
  17.                     <Spinner android:id="@+id/traderNameS" android:layout_width="fill_parent" android:layout_height="wrap_content">  
  18.                     </Spinner>  
  19.                 </TableRow>  
  20.                       
  21.                 <TableRow>  
  22.                     <TextView android:id="@+id/departName" android:text="@string/departName"  mce_/>  
  23.                     <Spinner android:id="@+id/departNameS" android:layout_width="fill_parent" android:layout_height="wrap_content"/>  
  24.                 </TableRow>  
  25.                   
  26.                 <TableRow >  
  27.                     <TextView android:id="@+id/strComponyName" android:text="@string/accType"  mce_/>  
  28.                     <Spinner android:id="@+id/accTyte" android:layout_width="fill_parent" android:layout_height="wrap_content"/>  
  29.                 </TableRow>  
  30.                   
  31.                 <TableRow >  
  32.                     <TextView android:id="@+id/strUserAcc" android:text="@string/userAcc"  mce_/>  
  33.                     <EditText android:id="@+id/accEditText" android:text="" android:numeric="decimal"  mce_/>  
  34.                 </TableRow>  
  35.                   
  36.                 <TableRow >  
  37.                     <TextView android:id="@+id/userPwd" android:text="@string/userPWD"  mce_/>  
  38.                     <EditText android:id="@+id/userPwdET" android:text="" android:password="true"  mce_/>  
  39.                 </TableRow>  
  40.                   
  41.                 <TableRow >  
  42.                     <TextView android:id="@+id/commPwd" android:text="@string/commPWD"  mce_/>  
  43.                     <EditText android:id="@+id/commPwdET" android:text="" android:password="true"  mce_/>  
  44.                 </TableRow>  
  45.             </TableLayout>  
  46.         </LinearLayout>  
  47.           
  48.         <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content">  
  49.               
  50.                 <LinearLayout android:id="@+id/linearLayoutTab1" android:layout_height="wrap_content"   
  51.                     android:layout_width="fill_parent" android:orientation="horizontal">  
  52.                       
  53.                     <LinearLayout android:id="@+id/linearLayoutTab2" android:layout_height="wrap_content"   
  54.                         android:layout_width="fill_parent" android:orientation="horizontal" android:layout_weight="1">  
  55.                         <CheckBox android:id="@+id/recordAcc"  android:layout_width="wrap_content" android:layout_height="wrap_content"/>  
  56.                         <TextView android:id="@+id/commPwd" android:text="@string/saveUserAcc"  mce_/>  
  57.                     </LinearLayout>     
  58.                     <LinearLayout android:id="@+id/linearLayoutTab2" android:layout_height="wrap_content"   
  59.                         android:layout_width="fill_parent" android:orientation="horizontal" android:layout_weight="1">  
  60.                           
  61.                         <CheckBox android:id="@+id/hideAcc" android:layout_width="wrap_content" android:layout_height="wrap_content"/>  
  62.                         <TextView android:id="@+id/commPwd" android:text="@string/ycUserAcc"  mce_/>  
  63.                     </LinearLayout>         
  64.                 </LinearLayout>  
  65.                   
  66.             <LinearLayout android:id="@+id/linearLayoutTab1" android:layout_height="wrap_content"   
  67.                     android:layout_width="fill_parent" android:orientation="horizontal">  
  68.               
  69.                     <LinearLayout android:layout_width="220px" android:layout_height="wrap_content" android:orientation="horizontal"   
  70.                         android:gravity="center">  
  71.                               
  72.                                 <Button android:id="@+id/confirmexch" android:gravity="center" android:layout_width="wrap_content"    
  73.                                     android:layout_height="50px" android:textSize="22dp" android:text="@string/login" android:layout_weight="1"  
  74.                                     android:focusable="false" android:textColor="@color/white" android:background="@drawable/buttonl"/>  
  75.                     </LinearLayout>  
  76.                   
  77.                     <LinearLayout android:id="@+id/linearLayoutTab" android:layout_height="wrap_content"   
  78.                         android:layout_width="fill_parent" android:gravity="center">  
  79.                         <ImageView android:layout_width="wrap_content" android:layout_height="50px" android:id="@+id/myImage"/>  
  80.                     </LinearLayout>  
  81.         </LinearLayout>  
  82.     </LinearLayout>  
  83.     </LinearLayout>  
  84. </ScrollView>  
Copyright © Linux教程網 All Rights Reserved