当前位置: 移动技术网 > IT编程>移动开发>Android > 解析android中include标签的使用

解析android中include标签的使用

2019年07月24日  | 移动技术网IT编程  | 我要评论

三人行必有我妹谁是妹妹,衡阳市党政门户网,7个月的宝宝发育标准

在一个项目中我们可能会需要用到相同的布局设计,如果都写在一个xml文件中,代码显得很冗余,并且可读性也很差,所以我们可以把相同布局的代码单独写成一个模块,然后用到的时候可以通过<include /> 标签来重用layout代码。
app_title.xml:
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<relativelayout android:id="@+id/titlelayout" android:layout_width="fill_parent" android:layout_height="wrap_content"
   android:background="@drawable/bt" xmlns:android="http://schemas.android.com/apk/res/android">

  <textview android:text="@string/login" android:id="@+id/title" android:textsize="20px" android:textcolor="@color/white"
   android:layout_width="wrap_content" android:layout_centerinparent="true" android:layout_height="wrap_content"/>  
  <button android:text="返回" android:id="@+id/refresh" android:layout_width="wrap_content" android:layout_height="wrap_content"
   android:layout_alignparentright="true" android:focusable="false" android:textcolor="@color/white"
   android:background="@drawable/okbutton" android:layout_marginright="3px"/>
</relativelayout>

app_tradelogin.xml:
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<!-- android:layout_below与 android:layout_above 是必须设置的,否则界面不规则,不设置android:layout_above本界面没有滑动效果-->
<scrollview android:layout_below="@+id/titlelayout" android:layout_above="@+id/appbottom" android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:layout_centerinparent="true" xmlns:android="http://schemas.android.com/apk/res/android">
 <linearlayout android:id="@+id/thewholelinearlayout" android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:paddingleft="10px" android:paddingtop="6px" android:paddingright="10px" >
  <linearlayout android:id="@+id/linearlayoutleft" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content">
   <tablelayout android:id="@+id/widget43" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android" android:stretchcolumns="1">
    <tablerow >
    <!--  引用定义好的textview样式,如果这里的属性和样式里定义的属性重复,则这里会替换样式里定义的属性-->
     <textview android:id="@+id/tradername" android:text="@string/tradername" style="@style/styletextinfo1" mce_style="@style/styletextinfo1"/>
     <spinner android:id="@+id/tradernames" android:layout_width="fill_parent" android:layout_height="wrap_content">
     </spinner>
    </tablerow>

    <tablerow>
     <textview android:id="@+id/departname" android:text="@string/departname" style="@style/styletextinfo1" mce_style="@style/styletextinfo1"/>
     <spinner android:id="@+id/departnames" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
    </tablerow>

    <tablerow >
     <textview android:id="@+id/strcomponyname" android:text="@string/acctype" style="@style/styletextinfo1" mce_style="@style/styletextinfo1"/>
     <spinner android:id="@+id/acctyte" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
    </tablerow>

    <tablerow >
     <textview android:id="@+id/struseracc" android:text="@string/useracc" style="@style/styletextinfo1" mce_style="@style/styletextinfo1"/>
     <edittext android:id="@+id/accedittext" android:text="" android:numeric="decimal" style="@style/styleedittext1" mce_style="@style/styleedittext1"/>
    </tablerow>

    <tablerow >
     <textview android:id="@+id/userpwd" android:text="@string/userpwd" style="@style/styletextinfo1" mce_style="@style/styletextinfo1"/>
     <edittext android:id="@+id/userpwdet" android:text="" android:password="true" style="@style/styleedittext1" mce_style="@style/styleedittext1"/>
    </tablerow>

    <tablerow >
     <textview android:id="@+id/commpwd" android:text="@string/commpwd" style="@style/styletextinfo1" mce_style="@style/styletextinfo1"/>
     <edittext android:id="@+id/commpwdet" android:text="" android:password="true" style="@style/styleedittext1" mce_style="@style/styleedittext1"/>
    </tablerow>
   </tablelayout>
  </linearlayout>

  <linearlayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content">

    <linearlayout android:id="@+id/linearlayouttab1" android:layout_height="wrap_content"
     android:layout_width="fill_parent" android:orientation="horizontal">

     <linearlayout android:id="@+id/linearlayouttab2" android:layout_height="wrap_content"
      android:layout_width="fill_parent" android:orientation="horizontal" android:layout_weight="1">
      <checkbox android:id="@+id/recordacc"  android:layout_width="wrap_content" android:layout_height="wrap_content"/>
      <textview android:id="@+id/commpwd" android:text="@string/saveuseracc" style="@style/styletextinfo1" mce_style="@style/styletextinfo1"/>
     </linearlayout> 
     <linearlayout android:id="@+id/linearlayouttab2" android:layout_height="wrap_content"
      android:layout_width="fill_parent" android:orientation="horizontal" android:layout_weight="1">

      <checkbox android:id="@+id/hideacc" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
      <textview android:id="@+id/commpwd" android:text="@string/ycuseracc" style="@style/styletextinfo1" mce_style="@style/styletextinfo1"/>
     </linearlayout>  
    </linearlayout>

   <linearlayout android:id="@+id/linearlayouttab1" android:layout_height="wrap_content"
     android:layout_width="fill_parent" android:orientation="horizontal">

     <linearlayout android:layout_width="220px" android:layout_height="wrap_content" android:orientation="horizontal"
      android:gravity="center">

              <button android:id="@+id/confirmexch" android:gravity="center" android:layout_width="wrap_content" 
         android:layout_height="50px" android:textsize="22dp" android:text="@string/login" android:layout_weight="1"
         android:focusable="false" android:textcolor="@color/white" android:background="@drawable/buttonl"/>
     </linearlayout>

     <linearlayout android:id="@+id/linearlayouttab" android:layout_height="wrap_content"
      android:layout_width="fill_parent" android:gravity="center">
      <imageview android:layout_width="wrap_content" android:layout_height="50px" android:id="@+id/myimage"/>
     </linearlayout>
  </linearlayout>
 </linearlayout>
 </linearlayout>
</scrollview>

app_bottom.xml:
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout android:id="@+id/appbottom" android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:layout_alignparentbottom="true"
  xmlns:android="http://schemas.android.com/apk/res/android">

 <linearlayout android:orientation="horizontal" android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:background="@drawable/light">
  <imageview  android:id="@+id/about" android:layout_width="30px" android:layout_height="wrap_content" android:src="@drawable/ttt"
    android:layout_alignparentleft="true"/>
  <textview android:id="@+id/light" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textcolor="@color/black"/>
 </linearlayout>

 <linearlayout android:orientation="horizontal" android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:background="@drawable/mainmenu">
  <button android:id="@+id/quotbutton" android:layout_weight="1" android:layout_width="52px" android:layout_height="wrap_content"
   android:textcolor="@color/white" android:text="" android:background="@drawable/button1"/>

  <button android:id="@+id/entrustbutton" android:layout_weight="1" android:layout_width="52px" android:layout_height="wrap_content"
   android:textcolor="@color/white" android:text="" android:background="@drawable/button1"/>
  <button android:id="@+id/querybutton" android:layout_weight="1" android:layout_width="52px" android:layout_height="wrap_content"
   android:textcolor="@color/white" android:text="" android:background="@drawable/button1"/>

  <button android:id="@+id/yinzhengbutton" android:layout_weight="1" android:layout_width="52px" android:layout_height="wrap_content"
   android:textcolor="@color/white" android:text="" android:background="@drawable/button1"/>

  <button android:id="@+id/recordbutton" android:layout_weight="1" android:layout_width="52px" android:layout_height="wrap_content"
   android:textcolor="@color/white" android:text="" android:background="@drawable/button1"/>

  <button android:id="@+id/logoutbutton" android:layout_weight="1" android:layout_width="52px" android:layout_height="wrap_content"
   android:textcolor="@color/white" android:text="" android:background="@drawable/button1"/>
 </linearlayout>
</linearlayout>

tradelogin_portrait.xml:
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<relativelayout android:layout_width="fill_parent" android:layout_height="fill_parent" style="@style/stylelayoutmain" mce_style="@style/stylelayoutmain"
 xmlns:android="http://schemas.android.com/apk/res/android">

 <!-- include标签内不能设置relativelayout属性,如android:layout_alignparentbottom,因为不起作用 -->
 <!-- include标签内设置id属性后(android:id),其引用的布局layout内的id属性就不起作用了,怀疑是其引用的layout外层包裹了一层include标签
  或者是覆盖了其内的属性id-->
 <!-- 如果没有include标签,所有布局代码都写在一个xml文件中,界面会显得很冗余,可读性很差。而且界面加载的时候是按照顺序加载的,前面的布局不能
  调用其后面的布局id。而采用include后,一个include中可以引用其后的include中的布局id属性 -->
 <include android:id="@id/titlelayout" layout="@layout/app_title" />
 <include layout="@layout/app_tradelogin"/>

 <include layout="@layout/app_bottom"/>

</relativelayout>

效果如下:

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网