zoukankan      html  css  js  c++  java
  • android中include标签的使用

    在一个项目中我们可能会需要用到相同的布局设计,如果都写在一个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>
    效果界面:

  • 相关阅读:
    JS LeetCode 1423. 可获得的最大点数简单题解
    SpringBoot 学集 (第六章) Docker
    Linux 学记 (第三章)
    Linux 学记 (第二章)
    Linux 学记 (第一章)
    SpringBoot 学集 (第五章) Web开发续
    SpringBoot 学集 (第四章)Web开发
    SpringBoot 学集 (第三章) 日志框架
    SpringBoot 学集 (第二章) 配置文件
    SpringBoot 学集 (第一章)
  • 原文地址:https://www.cnblogs.com/xkzy/p/3808186.html
Copyright © 2011-2022 走看看