
本文简述在AndroID开发中布局的简单应用,属于基础知识,仅供学习分享使用。
概述在AndroID UI开发中,布局类型主要有两种:linearLayout(线性布局)和relativeLayout(相对布局),两种布局类型各有各的优势与使用场景。
linearLayout(线性布局)线性布局允许所有的子元素,以单独的方向进行排列(水平或垂直),所有的元素像栈一样一个接一个的插入,所以如果是垂直(vertical)方向,则每一行只有一个元素。如果是水平( horizontal)方向,则只有一行。(如下图1所示)
线性布局重要属性androID:orIEntation 设置排列的方向。主要有两个值:horizontal(水平),vertical(垂直)。
androID:layout_weight 权重,按比例分配剩余空间。
(图1) (图2)
relativeLayout(相对布局)相对布局是指所有子元素以相对的位置进行定位。一个元素可以通过相对于指定的同级元素(如,左边,右边,上边,下边)进行定位,也可以通过父元素进行定位(如,布局控件的顶端,左端,右端,底部等)(如上图2 所示)。如果发现页面中有多个线性布局进行嵌套,那么你就应该用一个相对布局来替换它。
相对布局重要属性androID:layout_alignParenttop 是否位于父控件的顶部(true 或 false)androID:layout_alignParentBottom 是否位于父控件的底部(true 或 false)androID:layout_alignParentleft 是否位于父控件的左边(true 或 false)androID:layout_alignParentRight 是否位于父控件的右边(true 或 false)androID:layout_centerInParent 是否位于父控件的中心(true 或 false)androID:layout_toleftOf 位于指定控件的左边(值为控件的ID)androID:layout_toRightOf 位于指定控件的右边(值为控件的ID)androID:layout_above 位于指定控件的上边(值为控件的ID)androID:layout_below 位于指定控件的下边(值为控件的ID)实例截图如下图1所示为线性布局(相对简单),如下图2所示,为相对布局(相对复杂)
图3 图4
布局源程序线性布局
1 <?xml version="1.0" enCoding="utf-8"?> 2 <linearLayout 3 xmlns:androID="http://schemas.androID.com/apk/res/androID" 4 xmlns:tools="http://schemas.androID.com/tools" 5 androID:layout_wIDth="match_parent" 6 androID:layout_height 7 androID:paddingBottom="@dimen/activity_vertical_margin" 8 androID:paddingleft="@dimen/activity_horizontal_margin" 9 androID:paddingRight 10 androID:paddingtop 11 androID:orIEntation="vertical" 12 tools:context="com.hex.demolayout.linearactivity"> 13 TextVIEw 14 androID:ID="@+ID/tv_text" 15 androID:text="@string/tv_name" 16 androID:textSize="20dp" 17 androID:layout_margintop="5dp" 18 androID:textcolor="@color/colorBlue" 19 androID:layout_wIDth 20 androID:layout_height="wrap_content"/> 21 EditText 22 ="@+ID/txt_name" 23 androID:hint="@string/hint_name" 24 25 26 27 ="@+ID/tv_age" 28 ="@string/tv_age" 29 30 31 32 33 34 35 ="@+ID/txt_age" 36 37 38 39 40 ="@+ID/tv_sex" 41 ="@string/tv_sex" 42 43 44 45 46 47 RadioGroup 48 ="@+ID/rg_sex" 49 50 androID:orIEntation="horizontal" 51 52 53 Radiobutton 54 ="@+ID/rb_male" 55 androID:text="@string/male" 56 androID:textSize="20sp" 57 androID:checked="true" 58 androID:layout_wIDth="wrap_content" 59 androID:layout_height 60 61 ="@+ID/rb_female" 62 63 ="@string/female" 64 androID:layout_marginleft="30dp" 65 66 67 </RadioGroup 68 69 ="@+ID/tv_love" 70 ="@string/love" 71 72 73 74 75 76 77 androID:layout_wIDth 78 79 ="10dp" 80 ="horizontal" 81 CheckBox 82 ="@+ID/ck_basketball" 83 ="@string/basketball" 84 85 86 87 88 ="@+ID/ck_football" 89 ="@string/football" 90 91 92 93 94 95 ="@+ID/ck_game" 96 ="@string/game" 97 98 99 100 101 linearLayout102 103 ="@+ID/tv_school"104 ="@string/school"105 106 107 108 109 110 111 ="@+ID/txt_school"112 ="@string/hint_school"113 114 115 116 ="@+ID/tv_addr"117 ="@string/addr"118 119 120 121 122 123 124 ="@+ID/txt_addr"125 ="@string/hint_addr"126 127 128 button129 ="@+ID/bn_submit"130 ="@string/bn_submit"131 132 133 134 >VIEw Code
相对布局
relativeLayout="com.hex.demolayout.MainActivity" 12 ="@+ID/tv_Title" androID:layout_centerHorizontal androID:layout_alignParenttop 21 androID:layout_margin="3dp" 22 ="@string/nine_tip" 23 24 ="@+ID/tv_center"="@string/center" 26 27 androID:onClick="open"="@color/colorRed" androID:layout_centerInParent 33 34 ="@+ID/tv_east" 35 ="@string/east" 39 androID:layout_alignParentleft 40 androID:layout_centerVertical 43 44 ="@+ID/tv_west"="@string/west" 47 48 androID:layout_alignParentRight 53 54 ="@+ID/tv_north"="@string/north" androID:layout_alignParentBottom 60 61 63 64 ="@+ID/tv_south"="@string/south" 67 68 69 androID:layout_below="@ID/tv_Title" 73 74 ="@+ID/tv_east_south"="@string/east_south" 76 77 81 82 83 84 ="@+ID/tv_west_south"="@string/west_south" 87 88 93 94 ="@+ID/tv_east_north" 95 ="@string/east_north"101 ="@+ID/tv_west_north"="@string/west_north"110 111 112 113 ="@+ID/tv_xun"="@string/xun"115 116 ="@color/colorGreen"="@ID/tv_east_south"121 122 ="@+ID/tv_li"123 ="@string/li"124 ="@ID/tv_south"128 129 131 132 ="@+ID/tv_kun"="@string/kun"134 135 136 137 ="@ID/tv_west_south"138 139 140 141 142 ="@+ID/tv_Zen"143 ="@string/Zen"144 145 146 147 androID:layout_toRightOf="@ID/tv_east"148 149 150 151 152 ="@+ID/tv_dui"153 ="@string/dui"154 155 156 157 androID:layout_toleftOf="@ID/tv_west"158 159 160 161 162 ="@+ID/tv_gen"163 ="@string/gen"164 165 166 167 androID:layout_above="@ID/tv_east_north"168 169 170 171 ="@+ID/tv_kan"172 ="@string/kan"173 174 175 ="@ID/tv_north"176 177 178 179 180 181 ="@+ID/tv_qan"182 ="@string/qan"183 184 185 186 ="@ID/tv_west_north"187 androID:layout_alignRight188 189 190 191 ="@+ID/tv_mu"192 ="@string/mu"193 194 195 196 ="@ID/tv_xun"197 198 199 200 ="@+ID/tv_huo"201 ="@string/huo"202 203 204 205 ="@ID/tv_li"206 207 208 209 210 ="@+ID/tv_tu"211 ="@string/tu"212 213 214 215 ="@ID/tv_kun"216 217 218 219 220 ="@+ID/tv_mu2"221 222 223 224 225 ="@ID/tv_Zen"226 androID:layout_alignleft227 228 229 230 ="@+ID/tv_tu2"231 232 233 234 235 ="@ID/tv_center"236 237 238 239 240 ="@+ID/tv_jin"241 ="@string/jin"242 243 244 245 ="@ID/tv_dui"246 247 248 249 250 ="@+ID/tv_tu3"251 252 253 254 255 ="@ID/tv_gen"256 257 258 259 ="@+ID/tv_shui"260 ="@string/shui"261 262 263 ="@ID/tv_kan"264 265 266 267 268 269 ="@+ID/tv_jin2"270 271 272 273 ="@ID/tv_qan"274 275 276 277 278 relativeLayout>VIEw Code
备注
基础知识学习,从零开始。
总结以上是内存溢出为你收集整理的一起学Android之Layout全部内容,希望文章能够帮你解决一起学Android之Layout所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)