在一个项目中我们可能会需要用到相同的布局设计,如果都写在一个xml文件中,代码显得很冗余,并且可读性也很差,所以我们可以把相同布局的代码单独写成一个模块,然后用到的时候可以通过<include /> 标签来重用layout代码。
app_title.xml:
[size=1em][backcolor=rgb(108, 226, 108) !important][size=1em]1 [size=1em]2 [size=1em]3 [size=1em]4 [size=1em]5 [size=1em]6 [size=1em]7 [size=1em]8 [size=1em]9 [size=1em]10 | [size=1em][size=1em]<?xml version="1.0" encoding="utf-8"?> [size=1em]<RelativeLayout android:id="@+id/titleLayout" android:layout_width="fill_parent" android:layout_height="wrap_content" [size=1em] android:background="@drawable/bt" xmlns:android="http://schemas.android.com/apk/res/android"> [size=1em] [size=1em] <TextView android:text="@string/login" android:id="@+id/title" android:textSize="20px" android:textColor="@color/white" [size=1em] android:layout_width="wrap_content" android:layout_centerInParent="true" android:layout_height="wrap_content"/> [size=1em] <Button android:text="返回" android:id="@+id/refresh" android:layout_width="wrap_content" android:layout_height="wrap_content" [size=1em] android:layout_alignParentRight="true" android:focusable="false" android:textColor="@color/white" [size=1em] android:background="@drawable/okbutton" android:layout_marginRight="3px"/> [size=1em]</RelativeLayout> |
app_tradelogin.xml:
[size=1em][backcolor=rgb(108, 226, 108) !important][size=1em]1 [size=1em]2 [size=1em]3 [size=1em]4 [size=1em]5 [size=1em]6 [size=1em]7 [size=1em]8 [size=1em]9 [size=1em]10 [size=1em]11 [size=1em]12 [size=1em]13 [size=1em]14 [size=1em]15 [size=1em]16 [size=1em]17 [size=1em]18 [size=1em]19 [size=1em]20 [size=1em]21 [size=1em]22 [size=1em]23 [size=1em]24 [size=1em]25 [size=1em]26 [size=1em]27 [size=1em]28 [size=1em]29 [size=1em]30 [size=1em]31 [size=1em]32 [size=1em]33 [size=1em]34 [size=1em]35 [size=1em]36 [size=1em]37 [size=1em]38 [size=1em]39 [size=1em]40 [size=1em]41 [size=1em]42 [size=1em]43 [size=1em]44 [size=1em]45 [size=1em]46 [size=1em]47 [size=1em]48 [size=1em]49 [size=1em]50 [size=1em]51 [size=1em]52 [size=1em]53 [size=1em]54 [size=1em]55 [size=1em]56 [size=1em]57 [size=1em]58 [size=1em]59 [size=1em]60 [size=1em]61 [size=1em]62 [size=1em]63 [size=1em]64 [size=1em]65 [size=1em]66 [size=1em]67 [size=1em]68 [size=1em]69 [size=1em]70 [size=1em]71 [size=1em]72 [size=1em]73 [size=1em]74 [size=1em]75 [size=1em]76 [size=1em]77 [size=1em]78 [size=1em]79 [size=1em]80 [size=1em]81 [size=1em]82 [size=1em]83 [size=1em]84 | [size=1em][size=1em]<?xml version="1.0" encoding="utf-8"?> [size=1em]<!-- android:layout_below与 android:layout_above 是必须设置的,否则界面不规则,不设置android:layout_above本界面没有滑动效果--> [size=1em]<ScrollView android:layout_below="@+id/titleLayout" android:layout_above="@+id/appbottom" android:layout_width="fill_parent" [size=1em] android:layout_height="fill_parent" android:layout_centerInParent="true" xmlns:android="http://schemas.android.com/apk/res/android"> [size=1em] <LinearLayout android:id="@+id/theWholeLinearLayout" androidrientation="vertical" android:layout_width="fill_parent" [size=1em] android:layout_height="wrap_content" android:paddingLeft="10px" android:paddingTop="6px" android:paddingRight="10px" > [size=1em] <LinearLayout android:id="@+id/linearLayoutLeft" androidrientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> [size=1em] <TableLayout android:id="@+id/widget43" android:layout_width="fill_parent" [size=1em] android:layout_height="fill_parent" androidrientation="vertical" [size=1em] xmlns:android="http://schemas.android.com/apk/res/android" android:stretchColumns="1"> [size=1em] <TableRow > [size=1em] <!-- 引用定义好的TextView样式,如果这里的属性和样式里定义的属性重复,则这里会替换样式里定义的属性--> [size=1em] <TextView android:id="@+id/traderName" android:text="@string/traderName" style="@style/StyleTextInfo1" mce_style="@style/StyleTextInfo1"/> [size=1em] <Spinner android:id="@+id/traderNameS" android:layout_width="fill_parent" android:layout_height="wrap_content"> [size=1em] </Spinner> [size=1em] </TableRow> [size=1em] [size=1em] <TableRow> [size=1em] <TextView android:id="@+id/departName" android:text="@string/departName" style="@style/StyleTextInfo1" mce_style="@style/StyleTextInfo1"/> [size=1em] <Spinner android:id="@+id/departNameS" android:layout_width="fill_parent" android:layout_height="wrap_content"/> [size=1em] </TableRow> [size=1em] [size=1em] <TableRow > [size=1em] <TextView android:id="@+id/strComponyName" android:text="@string/accType" style="@style/StyleTextInfo1" mce_style="@style/StyleTextInfo1"/> [size=1em] <Spinner android:id="@+id/accTyte" android:layout_width="fill_parent" android:layout_height="wrap_content"/> [size=1em] </TableRow> [size=1em] [size=1em] <TableRow > [size=1em] <TextView android:id="@+id/strUserAcc" android:text="@string/userAcc" style="@style/StyleTextInfo1" mce_style="@style/StyleTextInfo1"/> [size=1em] <EditText android:id="@+id/accEditText" android:text="" android:numeric="decimal" style="@style/StyleEditText1" mce_style="@style/StyleEditText1"/> [size=1em] </TableRow> [size=1em] [size=1em] <TableRow > [size=1em] <TextView android:id="@+id/userPwd" android:text="@string/userPWD" style="@style/StyleTextInfo1" mce_style="@style/StyleTextInfo1"/> [size=1em] <EditText android:id="@+id/userPwdET" android:text="" android:password="true" style="@style/StyleEditText1" mce_style="@style/StyleEditText1"/> [size=1em] </TableRow> [size=1em] [size=1em] <TableRow > [size=1em] <TextView android:id="@+id/commPwd" android:text="@string/commPWD" style="@style/StyleTextInfo1" mce_style="@style/StyleTextInfo1"/> [size=1em] <EditText android:id="@+id/commPwdET" android:text="" android:password="true" style="@style/StyleEditText1" mce_style="@style/StyleEditText1"/> [size=1em] </TableRow> [size=1em] </TableLayout> [size=1em] </LinearLayout> [size=1em] [size=1em] <LinearLayout androidrientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> [size=1em] [size=1em] <LinearLayout android:id="@+id/linearLayoutTab1" android:layout_height="wrap_content" [size=1em] android:layout_width="fill_parent" androidrientation="horizontal"> [size=1em] [size=1em] <LinearLayout android:id="@+id/linearLayoutTab2" android:layout_height="wrap_content" [size=1em] android:layout_width="fill_parent" androidrientation="horizontal" android:layout_weight="1"> [size=1em] <CheckBox android:id="@+id/recordAcc" android:layout_width="wrap_content" android:layout_height="wrap_content"/> [size=1em] <TextView android:id="@+id/commPwd" android:text="@string/saveUserAcc" style="@style/StyleTextInfo1" mce_style="@style/StyleTextInfo1"/> [size=1em] </LinearLayout> [size=1em] <LinearLayout android:id="@+id/linearLayoutTab2" android:layout_height="wrap_content" [size=1em] android:layout_width="fill_parent" androidrientation="horizontal" android:layout_weight="1"> [size=1em] [size=1em] <CheckBox android:id="@+id/hideAcc" android:layout_width="wrap_content" android:layout_height="wrap_content"/> [size=1em] <TextView android:id="@+id/commPwd" android:text="@string/ycUserAcc" style="@style/StyleTextInfo1" mce_style="@style/StyleTextInfo1"/> [size=1em] </LinearLayout> [size=1em] </LinearLayout> [size=1em] [size=1em] <LinearLayout android:id="@+id/linearLayoutTab1" android:layout_height="wrap_content" [size=1em] android:layout_width="fill_parent" androidrientation="horizontal"> [size=1em] [size=1em] <LinearLayout android:layout_width="220px" android:layout_height="wrap_content" androidrientation="horizontal" [size=1em] android:gravity="center"> [size=1em] [size=1em] <Button android:id="@+id/confirmexch" android:gravity="center" android:layout_width="wrap_content" [size=1em] android:layout_height="50px" android:textSize="22dp" android:text="@string/login" android:layout_weight="1" [size=1em] android:focusable="false" android:textColor="@color/white" android:background="@drawable/buttonl"/> [size=1em] </LinearLayout> [size=1em] [size=1em] <LinearLayout android:id="@+id/linearLayoutTab" android:layout_height="wrap_content" [size=1em] android:layout_width="fill_parent" android:gravity="center"> [size=1em] <ImageView android:layout_width="wrap_content" android:layout_height="50px" android:id="@+id/myImage"/> [size=1em] </LinearLayout> [size=1em] </LinearLayout> [size=1em] </LinearLayout> [size=1em] </LinearLayout> [size=1em]</ScrollView> |
[size=1em]1 [size=1em]2 [size=1em]3 [size=1em]4 [size=1em]5 [size=1em]6 [size=1em]7 [size=1em]8 [size=1em]9 [size=1em]10 [size=1em]11 [size=1em]12 [size=1em]13 [size=1em]14 [size=1em]15 [size=1em]16 [size=1em]17 [size=1em]18 [size=1em]19 [size=1em]20 [size=1em]21 [size=1em]22 [size=1em]23 [size=1em]24 [size=1em]25 [size=1em]26 [size=1em]27 [size=1em]28 [size=1em]29 [size=1em]30 [size=1em]31 [size=1em]32 | [size=1em][size=1em]<?xml version="1.0" encoding="utf-8"?> [size=1em]<LinearLayout android:id="@+id/appbottom" androidrientation="vertical" android:layout_width="fill_parent" [size=1em] android:layout_height="wrap_content" android:layout_alignParentBottom="true" [size=1em] xmlns:android="http://schemas.android.com/apk/res/android"> [size=1em] [size=1em] <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" [size=1em] android:layout_height="wrap_content" android:background="@drawable/light"> [size=1em] <ImageView android:id="@+id/about" android:layout_width="30px" android:layout_height="wrap_content" android:src="@drawable/ttt" [size=1em] android:layout_alignParentLeft="true"/> [size=1em] <TextView android:id="@+id/light" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@color/black"/> [size=1em] </LinearLayout> [size=1em] [size=1em] <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" [size=1em] android:layout_height="wrap_content" android:background="@drawable/mainmenu"> [size=1em] <Button android:id="@+id/quotButton" android:layout_weight="1" android:layout_width="52px" android:layout_height="wrap_content" [size=1em] android:textColor="@color/white" android:text="" android:background="@drawable/button1"/> [size=1em] [size=1em] <Button android:id="@+id/entrustButton" android:layout_weight="1" android:layout_width="52px" android:layout_height="wrap_content" [size=1em] android:textColor="@color/white" android:text="" android:background="@drawable/button1"/> [size=1em] <Button android:id="@+id/queryButton" android:layout_weight="1" android:layout_width="52px" android:layout_height="wrap_content" [size=1em] android:textColor="@color/white" android:text="" android:background="@drawable/button1"/> [size=1em] [size=1em] <Button android:id="@+id/yinZhengButton" android:layout_weight="1" android:layout_width="52px" android:layout_height="wrap_content" [size=1em] android:textColor="@color/white" android:text="" android:background="@drawable/button1"/> [size=1em] [size=1em] <Button android:id="@+id/recordButton" android:layout_weight="1" android:layout_width="52px" android:layout_height="wrap_content" [size=1em] android:textColor="@color/white" android:text="" android:background="@drawable/button1"/> [size=1em] [size=1em] <Button android:id="@+id/logoutButton" android:layout_weight="1" android:layout_width="52px" android:layout_height="wrap_content" [size=1em] android:textColor="@color/white" android:text="" android:background="@drawable/button1"/> [size=1em] </LinearLayout> [size=1em]</LinearLayout> |
[size=1em]1 [size=1em]2 [size=1em]3 [size=1em]4 [size=1em]5 [size=1em]6 [size=1em]7 [size=1em]8 [size=1em]9 [size=1em]10 [size=1em]11 [size=1em]12 [size=1em]13 [size=1em]14 [size=1em]15 [size=1em]16 | [size=1em][size=1em]<?xml version="1.0" encoding="utf-8"?> [size=1em]<RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" style="@style/StyleLayoutMain" mce_style="@style/StyleLayoutMain" [size=1em] xmlns:android="http://schemas.android.com/apk/res/android"> [size=1em] [size=1em] <!-- include标签内不能设置RelativeLayout属性,如android:layout_alignParentBottom,因为不起作用 --> [size=1em] <!-- include标签内设置id属性后(android:id),其引用的布局layout内的id属性就不起作用了,怀疑是其引用的layout外层包裹了一层include标签 [size=1em] 或者是覆盖了其内的属性id--> [size=1em] <!-- 如果没有include标签,所有布局代码都写在一个xml文件中,界面会显得很冗余,可读性很差。而且界面加载的时候是按照顺序加载的,前面的布局不能 [size=1em] 调用其后面的布局id。而采用include后,一个include中可以引用其后的include中的布局id属性 --> [size=1em] <include android:id="@id/titleLayout" layout="@layout/app_title" /> [size=1em] <include layout="@layout/app_tradelogin"/> [size=1em] [size=1em] <include layout="@layout/app_bottom"/> [size=1em] [size=1em]</RelativeLayout> |
效果如下:
欢迎光临 因仑“3+1”工程特种兵精英论坛 (http://bbs.enlern.com/) | Powered by Discuz! X3.4 |