Android学习笔记(八)——四种基本布局

  //此系列博文是《第一行Android代码》的学习笔记,如有错漏,欢迎指正!

  布局是一种可用于放置很多控件的容器,它可以按照一定的规律调整内部控件的位置,或是嵌套子布局,从而编写出精美的界面。基本布局一般有四种,我们来逐个学习.

一、LinearLayout

  LinearLayout 又称作线性布局,是一种非常常用的布局。通过 android:orientation 属性指定可以指定排列的线性方向,默认的指定是 horizontal,此时控件就会在水平方向上排列。

  例如我们在布局文件里定义3个button,ProView如下:

  aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMoAAAB1CAIAAACAm+lfAAAK40lEQVR4nO3b61NS6x7Acf+z04ty5uixdsdLJgILUhIWl8AEt1SKKJZEedloetRExYPZrrHdqUlQa6eCyTVPKbgWy5o5ZlbTVNNlOufFefG02XhDEB4u+vvO78XzguqZ1meetQDNqvz5WqSp/nMUEUYVms7wke82Z5S7TkdoZFUxj/RsbCOpjGqEsl9OS9thdp2saGBFsqXa3tausKKwlVRY0dsSK4wpv2yZMtvxwgwrihOrMz1hwdEVN6+YYV1L6IkV16GFGxYcXXHwiuPE2sePWRvHKDwDR1esvODEigKWpNIoVhgrZKm/Zhk0WTG+MTxYj1khWGjg6Nojr1S8MUx/WBtswdG1F16pPbHS/G4YPiI5PNQnhBc8Zm0c8Y+jC+6M8fM62G8Mt4FVaRTD0ZUAXml5YqUcFho4uuLgBY9ZO8OCoys+XvDGcGdYYoVRrDDCt0Bx8sIFK1NOrG1sKX4MCUdXfLzSBVaaPGaF2xIr4KPURPACWFthiRVGEr7AjpMXPGZtCwuOrmTwOlCPWVsHvgXCxWv/wYryxIKjCy+vg/mYBUcXdl4AKzSiM/BQnzhe+wJWvHfDjUcX3BkTxAses+DowsVrn8GK6Y0hHF0p5JX5sPZkC77Axs0rTX9/Fd/dMDQkHF2YeaX58zuWEwvBIuHowskr8++GccBCAz97k2BeAplZIDMXlk+gRbnMXFA2US4zl0nNZVJz/qkJtAhbD5VJh/7Ot6HFKcnQcZ7tlGQITfj6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MIgf3lhCCkUir/s35+UlLRnz56JyUlPXkePHvXOa9euXQ6nkwTE5/N7dLqjR4+Wy2TK2lpPXhhCMpls9+7dnZ2dwCsM8s2Luo+p7XXmzJnTp0+rP/30pFyOIXTr9u24+PjNvEZHR6Ojo8nbUVFRg0NDGEJXr13LzMoieX3zzTdRUVFRUVEUr2m73YVhRpOJWofAi9P54OV5HysUisysrEsdHfHx8RcuXjSaTNHR0Z1XrojF4oqTJ73zUqvVQqGwQ6Ph8XidnZ09Oh2Px8MQuqLVvvLKKxSvhIQElUqlVCqzs7OBVxjkjRfhdnvex0aTSaVSqRobr1+/Tn7EYDQqa2vb2tqoJ3otLS3fzc6St50zM61tbdQf//LqVYVSSUqyjY93aDTkx1tbW8cnJsjbdofj7w0N51Qq6rkk8OJ0NHgFcYAXRwNeEIsBL4jFgBfEYsALYjHgBbGYV14EEXRY5MzNz8M3Q3Mxb7yePHmCQiAMw+7fvw//aSgXgzeiQSwGvCAWA14Qi/0PGhsRE8YQq34AAAAASUVORK5CYII=" alt="" />

  需要注意的是,如果 LinearLayout 的排列方向是 horizontal,内部的控件就绝对不能将宽度指定为 match_parent,因为这样的话单独一个控件就会将整个水平方向占满,其他的控件就没有可放置的位置了。同样的道理,如果 LinearLayout的排列方向是 vertical,内部的控件就不能将高度指定为 match_parent。

   布局文件中还有一个android:layout_gravity可以用于指定控件在布局中的对齐方式。android:layout_gravity 的可选值和 android:gravity 差不多。需要注意,当LinearLayout 的排列方向是 horizontal时, 只有垂直方向上的对齐方式才会生效, 因为此时水平方向上的长度是不固定的,每添加一个控件,水平方向上的长度都会改变,因而无法指定该方向上的对齐方式。同样的道理,当 LinearLayout 的排列方向是 vertical 时,只有水平方向上的对齐方式才会生效。

  如当我们在android:orientation 属性为vertical时,改变3个button的layout_gravity,可使ProView呈现如下图:

  aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAACCCAIAAACM+RDDAAALPElEQVR4nO3d+1PSax7Acf+zbWqm01amdnEEvK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967V1pa2me3i0Si6poaQ2NjKCyEcU1NzcGDB1f++WGAlWitD4u7drkdq6Ghob6+vvXq1Yu1tQjj+0NDxSUlq2E9fvyY+5gDPp8//OABwri3t1epUhFYs7OzfD6fz+dzsCY9HoQQy7LcFgiwErR1YIVeuwzDKFWqjs7OkpKStvZ2lmUFAoHVZtNqtX+/eDEyrNbWVolE0tnVVVRUZLVa++z2oqIihLGtu3vXrl0crOPHjxuNRoPBoFarAVZCFwnW8/n50GuXZVmj0Wj8+efbt2+TUxwsa2hstFgs3J04k8nk/fapGFPT02aLhfvxW729jMFADI2Pj3d2dZHTzWbz+NOnZO3xeC61tFwxGrn7iQArQdsErFgOwEq0ABZEJYAFUQlgQVQCWBCVABZEpYiwnj+PPSmMEcY+vx9empxYRYL18eNHHAchhF69epUKn3eQTMHbvyAqASyISgALohLAgqgEsCAqASyISgALohLAgqgEsCAqASyISv8HGTKvpiiZ+twAAAAASUVORK5CYII=" alt="" />

   LinearLayout 中还有一个重要属性:android:layout_weight。当我们把不同控件的weight设置为不同的值,系统会为其按weight的比例分配大小,例如我们在排列方向为水平时,声明一个EditText和Button如下: 

 <EditText
android:id="@+id/input_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:hint="Type something"
/>
<Button
android:id="@+id/send"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Send"
/>

  由于我们使用了 android:layout_weight 属性,此时控件的宽度就不应该再由 android:layout_width 来决定,所以我们把layout_width都指定为0dp,其ProView效果如下:

  aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMoAAABOCAIAAADuJh4xAAAJIklEQVR4nO2b21MaaRqH/c82F8nFHpId95DaSjYKk+jEiCeyo9nK7FRZO0mZzE1Moo7GAyAHNaC2Jm0JJILIYZWaCBJRaBQ8QRAt5dBNd+/FXnwzLCohKrSc3qd+F6/ddvMpj+/3dTeW3f/nT+ny4P8RpklLIl3JafpSGpu/mM5EGr49c+r/cbbU3T9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f39a2trsVgMwzCHwxEKhcRicSgUikQiFoslcRSazkiS9Hq9w8PDDMMoFAqCIEiSRLNqGr0ikUhfX5/f74/FYhaLhaKo9fX18fFxNEef1MtoNJ7Uy+v1SiSSw8NDn8/X3t5esnq1tbU9/fHHJYfj8ePHz549a25pGRgY+LC4iFJeXk4QhFKpvHTpEtLr4cOHZoulsamp59UrTvSKx+NqtTp5i1ardTqdqBaJRLOzs6Ojo3NzczRNsyy7vr6OYRiGYS6XK3EIwzALCwujo6OTk5OfPn1iWfbg4GBqakqlUi0sLDAMQ1HU9PQ0+maLxRIMBlmW9Xq9NpuNZdmtrS0Mw0ZHR9FNB4qiJicnTSbT3t6e0WhER2k0GpqmV1dXnU4nRVFTU1Nou9lsRq9otVoVCoVWq+3s7CxZvT4uL7c9eSKoq2ttbV1yODq7upqEwiahUCgUzi8s/Ov77wmCIDyeJ0+fjmMYNjHRJBR+29wslcnQ9uzrdQySJBUKBZoQ2aTJMc8Jh8M2m41hGK/XOzAwkOZBbNGQ7/e9ThIOhxUKRaJ1sSyL43hBXOTTND03NyeXy8fGxlAzK3oKTy+ggMh3vdxutzkVpnNhzCpzp8ZgMMyWDHt7ewWjF8MwNFBQFFL3Agoa0AvgkOAF6OV2p/m4PehVzESj0dXV1WWnk6ssL29ubqYZAOgFnJ8v3jsEvQAOAb0ADgG9AA4BvQAOAb0ADgG9AA4BvQAOAb0ADgG9AA4BvQAOAb0ADgG9AA4BvQAO+R8FsythqPMGYQAAAABJRU5ErkJggg==" alt="" />

  假如我们将Button的layout_width指定为“wrap_content”,而EditText的layout_weight指定为1,此时Button 的宽度仍然按照 wrap_content 来计算,而 EditText 则会占满屏幕所有的剩余空间。使用这种方式编写的界面,不仅在各种屏幕的适配方面会非常好,而且看起来也更加舒服。

二、RelativeLayout

  和 LinearLayout 相比,RelativeLayout 显得更加随意一些,它可以通过相对定位的方式让控件出现在布局的任何位置。

  比如我们添加了下面3个代码: 

 <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button 1" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button 2" /> <Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:text="Button 3" />

  代码的意思显而易见,layout_alignParentLeft、layout_alignParentTop、layout_alignParentRight、 layout_alignParentBottom、layout_centerInParent 这几个属性由其字面可知:与父布局的相应位置对应。PreView如下:

  aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADZCAIAAADqsOj3AAAMYUlEQVR4nO3b61OTZx7Gcf4Xp51hphPr0hkKAtKJrdINMQnxgEWiQtdYEgggFAgBFBCRQastB6ejYBW7FgREUQ4SF+UgRA4JyXOT1HbW3cqLTsdaizDsvtkXT5uFHCAQfkkI12euFwyNNkO+c+chiSHZ+fUrLO//y3K3zxevzr5MD6bJXXG19mXkrG7pJ1Y9dfYKS8usOZb+Jbb8QrxNKs91Up5URZrUGqpaMSl+xzO+8vvDFvhzE1ae3w8qnybleVWqLBxXawuLOCkPTqm6wEwKx5UXYa06qfrgvpzCceV1WF6cUkF8ObV0Ncc1OK48DwunlAdJqbNrVFk1ygz/P2AbZSGr+aVvc11O2ZPih+Nq1WH599WpQE1qSVU4rlYdlh9PqQB/7lu8z/CiqLdh4XJq6VR/HFd4HvQmrM39S5+LpLJrVDiuvAorIE8pvyfFD8fVmsLC5ZT7pHBceR/Wpv6lz2VSqqwaVVYN3sNZc1hB/imXNTz38UmpsmrwCRlvw/J7UgFyObW4KlUWXhT1Liwk5ZyUKqsmDW85exMWLqeck8JxRRvWprqcch7ew1n/sIIvKQ9PKRxXVGFtzsspHFeEYSEp+z7T4LJ9PcIKiqS8fe5belzhedDrsHA5hePK92FtsKRW9Usfjiu/hLXxk1pTVXjLmS6sAP2XonTPffal4bgiCyvAr9B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tIICwggbCABMICEggLSCAsIIGwgATCAhIIC0ggLCCBsIAEwgISCAtIICwggbCABMICEggLSCAsIIGwgATCAhIIC0ggLCCBsIAEwgISCAtIICwggbCABMICEggLSCAsIIGwgATCAhIIC0ggLCCBsIAEwgISCAtIICwggbCABMICEggLSCAsIIGwgATCAhIIC0ggLCCBsIAEwgISCAtIICwggbCABMICEggLSCAsIIGwgATCAhIIC0ggLCCBsIAEwgISCAtIICwggbCABMICEggLSCAsIIGwgATCAhIIC0ggLCCBsIDEZgnr1W+/vZiZ8ftmZmZev37t7x+GL2yKsObm5qatVo6xgBjHzc/P+/tHQm5ThDU7O+v/nv7c1NTUmzdv/P0jIYewfB6W2YywggTC8j2EhbBIICyERWKThnXv3r3m5ubbHR2TRiPHWE9v78joKMfYwOCg/uHD2x0dzX/ib2k3ODTEMdbZ2Xm9qWlicpJjrL+//0FfH//Xtre3my0W/utJo/F6U9O9+/cRVtByDkskEu3duzc1NfX9998fGx9PTk5uvHqVY6yyslKj0eh0uqNHj4aGhqrV6oozZ9RqdbxYHB0drVarb7W25ufni0SitLS0mJiYUYNBq9Vu3bp11GAwWyx/CQsbn5jg6/noo49OnDghFAovXbqEsIKTy7AaGhstHBcREdGn1zuExTGm1+sjIiPttz93/rzy+HGOsadjY6GhoXw9KpWqsrJSq9UKBAKlUrk4rKdjY2Xl5Rxjp0pLdTodwgpOLsOKjY394IMPdu7cabZYPA/rfldXdHQ0/83TFRXZ2dlarVadnv7hhx/evHnTHha/oeHh2NhY+xMlwgo27k4s/oumGzeSk5MbGho4xirOnMnMylomrKHh4XfeeYe/kMrLzz958qRWq83MzOy4cycmJkYgENjDGhsfj4uLu3btGq6xgpbLsD7Py6uprQ0LC7vf1VVaVrZv377rTU27du/++uuvlwmLY+zgJ59oNJq6urrw8PA+vZ4Pi2NMo9Fs2bKFD8tssUik0pTU1MarV5tbWhBWcHIOq6a2trik5FRpacedOxxjFo6rravLzc29+s03/A0MBkN1dbX99nc7O+1nj9Fkqqqqyi8o6O7p4Rhrbmn59ttvOcbGJyZKy8pMU1N8PadKS4tLSopLSr64cAFhBSe8juV7CAthkUBYCIsEwkJYJBAWwiKBsBAWiU0R1tzcnP29Yb/PZDLho8nB48WLF1wAsFgsMzMz/v5h+MJmCQt8DGEBCYQFJBAWkEBYQAJhAQmEBSQQFpBAWEACYQEJhAUkEBaQQFhA4n8aZ+HILR7n+wAAAABJRU5ErkJggg==" alt="" />

  我们也可以让控件相对于控件来定位:

 <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button 2" /> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/button2"
android:layout_toLeftOf="@id/button2"
android:text="Button 1" /> <Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/button2"
android:layout_toRightOf="@id/button2"
android:text="Button 3" />

  代码意思显而易见,其ProView如下:

  aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMkAAABOCAIAAAAFEaUyAAAIy0lEQVR4nO3c61NTZx7A8f4l+sKZ7TBjYb3MoFUCDGhFo+h2uFaDAiYIJg3CcjHDJSYkoWpcbgFEAYVALRHNjVAEYpRAJffLOQ+BnJl2RtGXrQZw2Tf74pQs64XWkmOS4+87zzDhcEh4mA8PDwfCZ6sQRE2fhfsDgGgb2IKoCmxBVAW2IKoCWxBVgS2IqsAWRFVgC6IqsAVRFdiCqApsQVQFtiCqAlsQVYEtiKpCYGt2rYi6vfl5QZssNLb+E2GBrUiInrY2Pylo84VmvxVuS28G61YkFOJ1a/P3FpKiyNa/IyMqphYyW6urq8+fP//5l18iYRiNxs3P6yO0+Pz5/MJCJIynT5+GfHYhsxUIBPwEESHD7fGsrKxsfmqUtry8jCMUIcPj9QYCgdBOMGT7rYiypdFowNaH2nr16lVoJ0jPdQtsgS2wFX5VYOvDhgf2W/SwtQr7rb8U2Prj3rdutba2CgQCoVC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cO21VVlW1KRS7du0yjI6KxOLMzMx+pfKrQ4e6uro2sIUjlPvNN3w+v729PT4+fmJykrSFI8Tn87dt20ba8mLY8RMnCgoLe2/dGlKpwBZte9tWm0JRV19/SSTSaLU4QhiOK9rbKyoqbt2+TZ5gsViuXr0aPF+n1wdXIJfbffny5eqLF38cG8MRGlKpBgcHcYTsDodILHZ7PCSgSyJRXX19XX39v5qawBZtg+tb4QpsgS2qAltgi6rAFtiiKrAFtqgKbIEtqqK/reXl5eCvkMM+3G43/E0zrXr27BkeAWEYtri4GO5Pxsfrk7AFhSWwBVEV2IKoCmxBVAW2IKoCWxBVgS2IqsAWRFVgC6Kq/wLI3pzviovvAwAAAABJRU5ErkJggg==" alt="" />

  不过注意,当一个控件去引用另一个控件的 id 时,该控件一定要定义在引用控件的后面,不然会出现找不到 id的情况。

三、FrameLayout

  FrameLayout 相比于前面两种布局就简单太多了,因此它的应用场景也少了很多。这种布局没有任何的定位方式,所有的控件都会摆放在布局的左上角,这里就不详细说明了。

四、TableLayout

  TableLayout 允许我们使用表格的方式来排列控件。设计表格时我们尽量应该让每一行都拥有相同的列数,这样的表格也是最简单的。而当表格的某行一定要有不相等的列数时,就需要通过合并单元格的方式来
应对。下面我们来设计一个登陆界面的布局文件:  

 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1"> <TableRow>
<TextView
android:layout_height="wrap_content"
android:text="Account:" />
<EditText
android:id="@+id/account"
android:layout_height="wrap_content"
android:hint="Input your account" />
</TableRow>
<TableRow>
<TextView
android:layout_height="wrap_content"
android:text="Password:" />
<EditText
android:id="@+id/password"
android:layout_height="wrap_content"
android:inputType="textPassword" />
</TableRow>
<TableRow>
<Button
android:id="@+id/login"
android:layout_height="wrap_content"
android:layout_span="2"
android:text="Login" />
</TableRow>
</TableLayout>

  TableLayout 中每加入一个 TableRow 就表示在表格中添加了一行,然后在 TableRow中每加入一个控件,就表示在该行中加入了一列,TableRow 中的控件是不能指定宽度的。这里我们将表格设计成了三行两列的格式,第一行有一个 TextView 和一个用于输入账号的EditText,第二行也有一个 TextView 和一个用于输入密码的 EditText,我们通过将android:inputType 属性的值指定为 textPassword,使EditText变为密码输入框。第三行只有一个用于登录的按钮,前两行都有两列,第三行只有一列,所以我们使用android:layout_span="2"让登录按钮占据两列的空间。 另外,由于 在 TableRow 中 我 们 无 法 指 定 控 件 的 宽 度 ,可能导致每一行都不能充满屏幕的宽度。 这 时 可以使 用android:stretchColumns 属性让某一列控件拉伸以充满整个屏幕的宽度。代码的ProView如下:

  aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMsAAABXCAIAAAAlPSZcAAAMDUlEQVR4nO2ca08bxxqA/Y/6rVIqVZV6Gp0epFRKT6JTqTpVpapS+w9ootOqSkuSUkHAFEpshxhjG/AFX1jjNRh8ARtj8AVsr/EVc/ElBowveHdn+HA+TENpSi4Q1mvDPHo/jNe76/edfZhdBsYCeOkAmGZCwLcPFwnfnYk5hctjGN89iTmdljeM7w7EvIbWNozv3sO8nlY1jO9+w7wprWcY3z2GORv8GJbP5xcWFs56FMqYZdlqtcpvr52Pw8PDer3OdxaNRgAhZFmWA4teRTKZlMlkZzrkOONsNvvkyRMeu+zckCRps9n4zqLRCBiGGRgY2N3dvRB1EolEPp9/k92QYQsLC9PT08PDw0NDQ7lcDkKoVqtNJpNYLNbpdDRN53I5i8WCch0dHd3d3ZVIJA8ePCBJEm0sl8sKhQK1CYLY2NioVqtarVYsFo+Pjx8cHAAAVCpVpVIBAPj9fr/fjw6RyWQej+e4I5xOp0QikUgksVgMAFAqlZRKpUgk0mq19XqdZVmSJIVC4eDg4NbWFgAgGo329/f39fUhaba2toxGIzqVRCIBADgcDoIgBgcHe3t7U6mU3++/f//+w4cPA4FAA65r8yCIxWIWi8Vut6NxwufzkSSZyWQghNFolCTJSCQCIdzZ2bFYLIuLiyzLplIppJHX6wUAeDye2dlZp9O5v7+vUCgmJiZYlvV4PG9iG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GHaZ3MKGNUn8aVgjB7AGuIX1apL4869G1HO4/cjTCHMToXAYB+/hDwTq9boAAHBwcHDu//ZPNx8pTBOQTCZ3d3cBXs2G4RpsGIZbsGEYbsGGYbgFG4bhFmwYhluwYRhuwYZhuAUbhuEWbBiGW7BhGG7BhmG4BRuG4RZsGIZb/g8TIicthnqpYgAAAABJRU5ErkJggg==" alt="" />

  //End.

上一篇:【转】 Pro Android学习笔记(八十):服务(5):访问远程服务


下一篇:Linux开放防火墙指定端口