android中include標籤的使用

來源:互聯網
上載者:User

標籤:android   http   tar   ext   color   com   

在一個項目中我們可能會需要用到相同的布局設計,如果都寫在一個xml檔案中,代碼顯得很冗餘,並且可讀性也很差,所以我們可以把相同布局的代碼單獨寫成一個模組,然後用到的時候可以通過<include /> 標籤來重用layout代碼。app_title.xml: 01<?xml version="1.0" encoding="utf-8"?>02<RelativeLayout android:id="@+id/titleLayout" android:layout_width="fill_parent" android:layout_height="wrap_content"03   android:background="@drawable/bt" xmlns:android="http://schemas.android.com/apk/res/android">04    05  <TextView android:text="@string/login" android:id="@+id/title" android:textSize="20px" android:textColor="@color/white"06   android:layout_width="wrap_content" android:layout_centerInParent="true" android:layout_height="wrap_content"/>  07  <Button android:text="返回" android:id="@+id/refresh" android:layout_width="wrap_content" android:layout_height="wrap_content"08   android:layout_alignParentRight="true" android:focusable="false" android:textColor="@color/white"09   android:background="@drawable/okbutton" android:layout_marginRight="3px"/>10</RelativeLayout>app_tradelogin.xml: 01<?xml version="1.0" encoding="utf-8"?>02<!-- android:layout_below與 android:layout_above 是必須設定的,否則介面不規則,不設定android:layout_above本介面沒有滑動效果-->03<ScrollView android:layout_below="@+id/titleLayout" android:layout_above="@+id/appbottom" android:layout_width="fill_parent" 04  android:layout_height="fill_parent" android:layout_centerInParent="true" xmlns:android="http://schemas.android.com/apk/res/android">05 <LinearLayout android:id="@+id/theWholeLinearLayout" android:orientation="vertical" android:layout_width="fill_parent" 06  android:layout_height="wrap_content" android:paddingLeft="10px" android:paddingTop="6px" android:paddingRight="10px" >07  <LinearLayout android:id="@+id/linearLayoutLeft" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content">08   <TableLayout android:id="@+id/widget43" android:layout_width="fill_parent"09    android:layout_height="fill_parent" android:orientation="vertical"10    xmlns:android="http://schemas.android.com/apk/res/android" android:stretchColumns="1">11    <TableRow > 12    <!--  引用定義好的TextView樣式,如果這裡的屬性和樣式裡定義的屬性重複,則這裡會替換樣式裡定義的屬性-->13     <TextView android:id="@+id/traderName" android:text="@string/traderName" mce_/>14     <Spinner android:id="@+id/traderNameS" android:layout_width="fill_parent" android:layout_height="wrap_content">15     </Spinner>16    </TableRow>17      18    <TableRow>19     <TextView android:id="@+id/departName" android:text="@string/departName" mce_/>20     <Spinner android:id="@+id/departNameS" android:layout_width="fill_parent" android:layout_height="wrap_content"/>21    </TableRow>22     23    <TableRow >24     <TextView android:id="@+id/strComponyName" android:text="@string/accType" mce_/>25     <Spinner android:id="@+id/accTyte" android:layout_width="fill_parent" android:layout_height="wrap_content"/>26    </TableRow>27     28    <TableRow >29     <TextView android:id="@+id/strUserAcc" android:text="@string/userAcc" mce_/>30     <EditText android:id="@+id/accEditText" android:text="" android:numeric="decimal" mc_/>31    </TableRow>32     33    <TableRow >34     <TextView android:id="@+id/userPwd" android:text="@string/userPWD" mce_/>35     <EditText android:id="@+id/userPwdET" android:text="" android:password="true" mce_/>36    </TableRow>37     38    <TableRow >39     <TextView android:id="@+id/commPwd" android:text="@string/commPWD" mce_/>40     <EditText android:id="@+id/commPwdET" android:text="" android:password="true" mce_/>41    </TableRow>42   </TableLayout>43  </LinearLayout>44   45  <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content">46    47    <LinearLayout android:id="@+id/linearLayoutTab1" android:layout_height="wrap_content" 48     android:layout_width="fill_parent" android:orientation="horizontal">49      50     <LinearLayout android:id="@+id/linearLayoutTab2" android:layout_height="wrap_content" 51      android:layout_width="fill_parent" android:orientation="horizontal" android:layout_weight="1">52      <CheckBox android:id="@+id/recordAcc"  android:layout_width="wrap_content" android:layout_height="wrap_content"/>53      <TextView android:id="@+id/commPwd" android:text="@string/saveUserAcc" mce_/>54     </LinearLayout> 55     <LinearLayout android:id="@+id/linearLayoutTab2" android:layout_height="wrap_content" 56      android:layout_width="fill_parent" android:orientation="horizontal" android:layout_weight="1">57       58      <CheckBox android:id="@+id/hideAcc" android:layout_width="wrap_content" android:layout_height="wrap_content"/>59      <TextView android:id="@+id/commPwd" android:text="@string/ycUserAcc" mce_/>60     </LinearLayout>  61    </LinearLayout>62     63   <LinearLayout android:id="@+id/linearLayoutTab1" android:layout_height="wrap_content" 64     android:layout_width="fill_parent" android:orientation="horizontal">65    66     <LinearLayout android:layout_width="220px" android:layout_height="wrap_content" android:orientation="horizontal" 67      android:gravity="center">68              69              <Button android:id="@+id/confirmexch" android:gravity="center" android:layout_width="wrap_content"  70         android:layout_height="50px" android:textSize="22dp" android:text="@string/login" android:layout_weight="1"71         android:focusable="false" android:textColor="@color/white" android:background="@drawable/buttonl"/>72     </LinearLayout>73     74     <LinearLayout android:id="@+id/linearLayoutTab" android:layout_height="wrap_content" 75      android:layout_width="fill_parent" android:gravity="center">76      <ImageView android:layout_width="wrap_content" android:layout_height="50px" android:id="@+id/myImage"/>77     </LinearLayout>78  </LinearLayout>79 </LinearLayout>80 </LinearLayout>81</ScrollView>app_bottom.xml: 01<?xml version="1.0" encoding="utf-8"?>02<LinearLayout android:id="@+id/appbottom" android:orientation="vertical" android:layout_width="fill_parent" 03  android:layout_height="wrap_content" android:layout_alignParentBottom="true"04  xmlns:android="http://schemas.android.com/apk/res/android">05   06 <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" 07  android:layout_height="wrap_content" android:background="@drawable/light">08  <ImageView  android:id="@+id/about" android:layout_width="30px" android:layout_height="wrap_content" android:src="@drawable/ttt"09    android:layout_alignParentLeft="true"/>http://www.huiyi8.com/webyuanma/10  <TextView android:id="@+id/light" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@color/black"/>11 </LinearLayout>12  13 <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" 14  android:layout_height="wrap_content" android:background="@drawable/mainmenu">15  <Button android:id="@+id/quotButton" android:layout_weight="1" android:layout_width="52px" android:layout_height="wrap_content"16   android:textColor="@color/white" android:text="" android:background="@drawable/button1"/>17    18  <Button android:id="@+id/entrustButton" android:layout_weight="1" android:layout_width="52px" android:layout_height="wrap_content"19   android:textColor="@color/white" android:text="" android:background="@drawable/button1"/>20  <Button android:id="@+id/queryButton" android:layout_weight="1" android:layout_width="52px" android:layout_height="wrap_content"21   android:textColor="@color/white" android:text="" android:background="@drawable/button1"/>22    23  <Button android:id="@+id/yinZhengButton" android:layout_weight="1" android:layout_width="52px" android:layout_height="wrap_content"24   android:textColor="@color/white" android:text="" android:background="@drawable/button1"/>25    26  <Button android:id="@+id/recordButton" android:layout_weight="1" android:layout_width="52px" android:layout_height="wrap_content"27   android:textColor="@color/white" android:text="" android:background="@drawable/button1"/>28    29  <Button android:id="@+id/logoutButton" android:layout_weight="1" android:layout_width="52px" android:layout_height="wrap_content"30   android:textColor="@color/white" android:text="" android:background="@drawable/button1"/>31 </LinearLayout>網站源碼下載32</LinearLayout>tradelogin_portrait.xml: view sourceprint?01<?xml version="1.0" encoding="utf-8"?>02<RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" mc_03 xmlns:android="http://schemas.android.com/apk/res/android">04  05 <!-- include標籤內不能設定RelativeLayout屬性,如android:layout_alignParentBottom,因為不起作用 -->06 <!-- include標籤內設定id屬性後(android:id),其引用的布局layout內的id屬性就不起作用了,懷疑是其引用的layout外層包裹了一層include標籤 07  或者是覆蓋了其內的屬性id-->08 <!-- 如果沒有include標籤,所有布局代碼都寫在一個xml檔案中,介面會顯得很冗餘,可讀性很差。而且介面載入的時候是按照順序載入的,前面的布局不能09  調用其後面的布局id。而採用include後,一個include中可以引用其後的include中的布局id屬性 -->10 <include android:id="@id/titleLayout" layout="@layout/app_title" />11 <include layout="@layout/app_tradelogin"/>12   13 <include layout="@layout/app_bottom"/>14  15</RelativeLayout>效果介面:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.