<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ## Android布局管理器 **Android的Activity**組件通過setContentView(xml resId)?來為activity綁定顯示界面,然而為了更好的更方便的管理Android應用的用戶界面里面的各種控件(按鈕? 文本框? 編輯框 圖片等一系列組件),Android向我們提供了布局管理器。通過使用布局管理器,Android的圖形界面跟java代碼間具有很好 的 無關性,使代碼與布局分開,這樣就能減少代碼量,代碼只需要做一些界面組件的控制和一些數據處理,這樣就有點類似java 中的MVC( modle??? view??? control)結構,android中的布局管理器就起到了View的作用,而java代碼就起到了control的作用,android中的數據存儲就起到了modle的作用。介紹完了布局管理器是什么了以后,接下來就介紹布局管理器有哪幾類,布局管理器有:線性布局(LinearLayout)?? 相對布局(RelativeLayout)? 網格布局(GridLayout)?幀布局(FrameLayout)? 絕對布局(AbsoluteLayout)?? 接下來一一介紹各種布局管理器的作用。 ? 1. **線性布局(LinearLayout)** 首先來到官方文檔,看看官方文檔的介紹(一定要會看官方文檔)如下 ![](https://box.kancloud.cn/2016-04-26_571f42c617953.jpg) 官方大意為:在一個單一的列或一行排列其子的布局。該行的方向可以通過調用setorientation()來設置水平還是垂直。你也可以指定排列方式,通過設置LinearLayout.layoutparams來指定所排列的所有子元素調用setgravity()或指定特定的孩子的權重來填補剩余的空間布局。默認方向為水平。? 還有一點非常重要,那就是LinearLayout中的組件排列起來的沿排列方向超出了屏幕的寬度,多出的部分是不會顯示出來的。 再來看看LinearLayout的XML屬性及相關方法,如下表:官方文檔 ![](https://box.kancloud.cn/2016-04-26_571f42c62b14d.jpg) 接下來就跟著官方文檔的屬性介紹單個詳細介紹: ![](https://box.kancloud.cn/2016-04-26_571f42c66e302.jpg) 該屬性設置為false,將會阻止該布局管理器與它的子元素的基線對齊。當它的子元素使用不同的權重值時這個熟悉特別有用,默認是true。 必須是Boolean值,是true或者false; ![](https://box.kancloud.cn/2016-04-26_571f42c683835.jpg) ![](https://box.kancloud.cn/2016-04-26_571f42c698e71.jpg) ????????????? 設置垂直布局時兩個按鈕之間的分割條 ![](https://box.kancloud.cn/2016-04-26_571f42c6ae8a9.jpg) 設置布局管理器內組件的對齊方式,該屬性支持top、bottom、left、right、center_vertical、fill_vertical、center_horizoontal、fill_horizontal、center、fill、clip_vertical、clip_horizontal這幾個屬性值,也可以同時指定多種組合,比如:left | center_vertical 代表的是出現在屏幕的左邊,而且垂直居中; ![](https://box.kancloud.cn/2016-04-26_571f42c6cb9bd.jpg) 當該屬性設置為true時,所有帶權重的子元素都會具有最大子元素的最小尺寸 ![](https://box.kancloud.cn/2016-04-26_571f42c6e21ba.jpg) 設置布局管理器內部組件的排列方式,可以設置為horizontal(水平)、vertical(垂直)之一,默認是vertical; ![](https://box.kancloud.cn/2016-04-26_571f42c7024d8.jpg) 在LinearLayout的XML中,舉個例子:android:weightSum="5" 表示這個LinearLayout總共平分成5塊大小區域;然后再LinearLayout里面的控件,使用android:layout_wetght=“1”,這表示它占用整個布局的1/5。 通過介紹了上面LinearLayout的屬性后,我們在通過下面的幾個例子來展示這個幾個屬性的使用: ![](https://box.kancloud.cn/2016-04-26_571f42c714dba.jpg) ![](https://box.kancloud.cn/2016-04-26_571f42c7311d8.jpg) ![](https://box.kancloud.cn/2016-04-26_571f42c74e528.jpg) ![](https://box.kancloud.cn/2016-04-26_571f42c76880f.jpg) ![](https://box.kancloud.cn/2016-04-26_571f42c7847a9.jpg) ![](https://box.kancloud.cn/2016-04-26_571f42c7a3857.jpg) ![](https://box.kancloud.cn/2016-04-26_571f42c7c335b.jpg) ![](https://box.kancloud.cn/2016-04-26_571f42c7dedc8.jpg) ![](https://box.kancloud.cn/2016-04-26_571f42c7dedc8.jpg) 通過上面幾個例子的展示,可以很只管的了解LinearLayout的那幾個屬性的使用效果。 2.?相對布局 (RelativeLayout) 通過Linearlayout的官方文檔介紹方法,大家可以上Android官網去查看,就按照上面的步驟學習其他組件。后面幾個布局管理器就不以上面的方式講解,就直接以簡短明了的方式向大家介紹。 相對布局容器內部的子組件的位置總是相對兄弟組件、父容器來決定的,因此這種布局方式被稱之為相對布局,例如:如果B組件的位置要由A組件的位置來決定的話,那么就必須先定義B組件并且,確定B的位置后,才能定義A組件。 RelativeLayout的XML屬性及相關方法說明 <table border="1" cellspacing="0" cellpadding="0"><tbody><tr><td valign="top"><p align="left">????? XML屬性</p></td><td valign="top"><p align="left">????? Java方法</p></td><td valign="top"><p align="left">?????? 說明</p></td></tr><tr><td valign="top"><p align="left"><span><span style="font-size:14px">android:gravity</span></span></p></td><td valign="top"><p align="left">setGravity(int)</p></td><td valign="top"><p align="left">設置該布局容器內各子組件的對齊方式</p></td></tr><tr><td valign="top"><p align="left">Android:ignoreGravity</p></td><td valign="top"><p align="left">setIgnoreGravity(int)</p></td><td valign="top"><p align="left">設置哪個組件不收gravity屬性影響</p></td></tr></tbody></table> 為了能夠控制該布局容器中各個子組件的布局分布,Relative也提供了一個內部類:RelativeLayout.LayoutParams,該類提供了大量的XML屬性來控制Relative布局容器中子組件的布局分布,它的xml屬性如下表所示: 下面的值只能是true或false <table border="1" cellspacing="0" cellpadding="0"><tbody><tr><td valign="top"><p align="left">android:layout_centerHorizontal</p></td><td valign="top"><p align="left">控制該子組件是否位于布局容器的水平居中</p></td></tr><tr><td valign="top"><p align="left">android:layout_centerVertical</p></td><td valign="top"><p align="left">控制該子組件是否位于布局容器的垂直居中</p></td></tr><tr><td valign="top"><p align="left">android:layout_centerInparent</p></td><td valign="top"><p align="left">控制該子組件是否位于布局容器的中央位置</p></td></tr><tr><td valign="top"><p align="left">android:layout_alignParentBottom</p></td><td valign="top"><p align="left">控制該子組件是否于布局容器底部對齊</p></td></tr><tr><td valign="top"><p align="left">android:layout_alignParentLeft</p></td><td valign="top"><p align="left">控制該子組件是否于布局容器左邊對齊</p></td></tr><tr><td valign="top"><p align="left">android:layout_alignParentRight</p></td><td valign="top"><p align="left">控制該子組件是否于布局容器右邊對齊</p></td></tr><tr><td valign="top"><p align="left">android:layout_alignParentTop</p></td><td valign="top"><p align="left">控制該子組件是否于布局容器頂端對齊</p></td></tr></tbody></table> 下面的屬性的值只能是其他UI組件的id值 <table border="1" cellspacing="0" cellpadding="0"><tbody><tr><td valign="top"><p align="left">android:layout_toRightOf</p></td><td valign="top"><p align="left">控制該子組件位于給出id組件的右側</p></td></tr><tr><td valign="top"><p align="left">android:layout_toLeftOf</p></td><td valign="top"><p align="left">控制該子組件位于給出id組件的左側</p></td></tr><tr><td valign="top"><p align="left">android:layout_above</p></td><td valign="top"><p align="left">控制該子組件位于給出id組件的上方</p></td></tr><tr><td valign="top"><p align="left">android:layout_below</p></td><td valign="top"><p align="left">控制該子組件位于給出id組件下方</p></td></tr><tr><td valign="top"><p align="left">android:layout_alignTop</p></td><td valign="top"><p align="left">控制該子組件位于給出id組件的上邊界對齊</p></td></tr><tr><td valign="top"><p align="left">android:layout_alignBottom</p></td><td valign="top"><p align="left">控制該子組件位于給出id組件的下邊界對齊</p></td></tr><tr><td valign="top"><p align="left">android:layout_alignLeft</p></td><td valign="top"><p align="left">控制該子組件位于給出id組件的左邊界對齊</p></td></tr><tr><td valign="top"><p align="left">android:layout_alignRight</p></td><td valign="top"><p align="left">控制該子組件位于給出id組件的右邊界對齊</p></td></tr></tbody></table> ? 下面是通過一個雪花效果來展示Relative的具體使用效果 ~~~ <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000"> <!--定義該組件位于父容器的中央--> <TextView android:id="@+id/xuehua_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@drawable/iconfont_xuehua" /> <!--定義該組件位于view1的上方--> <TextView android:id="@+id/xuehua_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/xuehua_1" android:layout_alignLeft="@id/xuehua_1" android:background="@drawable/iconfont_xuehua" /> <!--定義該組件位于view1的下方--> <TextView android:id="@+id/xuehua_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@id/xuehua_1" android:layout_below="@id/xuehua_1" android:background="@drawable/iconfont_xuehua" /> <!--定義該組件位于view1的左方--> <TextView android:id="@+id/xuehua_4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@id/xuehua_1" android:layout_toLeftOf="@id/xuehua_1" android:background="@drawable/iconfont_xuehua" /> <!--定義該組件位于view1的右方--> <TextView android:id="@+id/xuehua_5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@id/xuehua_1" android:layout_toRightOf="@id/xuehua_1" android:background="@drawable/iconfont_xuehua" /> <!--定義該組件位于父容器的上方中央--> <TextView android:id="@+id/xuehua_6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:background="@drawable/iconfont_xuehua" /> <!--定義該組件位于父容器的左邊中央--> <TextView android:id="@+id/xuehua_7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:background="@drawable/iconfont_xuehua" /> <!--定義該組件位于父容器的右邊中央--> <TextView android:id="@+id/xuehua_8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:background="@drawable/iconfont_xuehua" /> <!--定義該組件位于父容器的下邊居中--> <TextView android:id="@+id/xuehua_9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:background="@drawable/iconfont_xuehua" /> </RelativeLayout> ~~~ 下面是效果圖: ![](https://box.kancloud.cn/2016-04-26_571f42c811b00.jpg) 3. **網格布局(GridLayout)** **網格布局是 Android4.0以后新推出的布局管理器,顧名思義,網格布局就是把整個容器劃分為rows X columns個網格,每個網格放一個組件,除此之外還可以設置一個組件橫跨多少列、一個組件縱跨多少行。gridlayout的xml屬性和相關方法如下:** **** <table border="1" cellspacing="0" cellpadding="0"><tbody><tr><td valign="top"><p align="left">Xml屬性</p></td><td valign="top"><p align="left">相關方法</p></td><td valign="top"><p align="left">說明</p></td></tr><tr><td valign="top"><p align="left">Andorid:alignmentMode</p></td><td valign="top"><p align="left">setAlignmentMode(int)</p></td><td valign="top"><p align="left">設置該布局管理器采用的對齊方式</p></td></tr><tr><td valign="top"><p align="left">Android:columnCount</p></td><td valign="top"><p align="left">setColumnCount(int)</p></td><td valign="top"><p align="left">設置該網格的列數量</p></td></tr><tr><td valign="top"><p align="left">Android:columnOrderPreserved</p></td><td valign="top"><p align="left">setColumnOrderPreserved(boolean)</p></td><td valign="top"><p align="left">設置該網格空容器是否保留列序號</p></td></tr><tr><td valign="top"><p align="left">Android:rowCount</p></td><td valign="top"><p align="left">setRowCount(int)</p></td><td valign="top"><p align="left">設置該網格的行數量</p></td></tr><tr><td valign="top"><p align="left">Android:rowOrderPreserved</p></td><td valign="top"><p align="left">setRowOrderPreserved(boolean)</p></td><td valign="top"><p align="left">設置該網格容器是否保留行序號</p></td></tr><tr><td valign="top"><p align="left">Androi:useDefaultMargins</p></td><td valign="top"><p align="left">setDefaultMargins(boolean)</p></td><td valign="top"><p align="left">設置該布局管理器是否使用默認的頁邊距</p></td></tr></tbody></table> ??? 為了控制GridLayout布局容器中各子組件的布局分布,GridLayout也提供了一個內部類:GridLayouut.Params,該類提供了大量的XML屬性來控制GridLayout布局容器中子組件的布局分布。 ?????? GridLayout.LayoutParams的XML屬性 ? <table border="1" cellspacing="0" cellpadding="0"><tbody><tr><td valign="top"><p align="center">Xml屬性</p></td><td valign="top"><p align="center">相關方法</p></td><td valign="top"><p align="center">說明</p></td></tr><tr><td valign="top"><p align="center">Android:layout_column</p></td><td valign="top"><p align="center">?</p></td><td valign="top"><p align="center">設置該子組件在GridLayout的第幾列</p></td></tr><tr><td valign="top"><p align="center">Android:layout_columSpan</p></td><td valign="top"><p align="center">?</p></td><td valign="top"><p align="center">設置該子組件在GridLayout橫向上跨幾列</p></td></tr><tr><td valign="top"><p align="center">Android:layout_gravity</p></td><td valign="top"><p align="center">setGravity(int)</p></td><td valign="top"><p align="center">設置該子組件采用何種方式占據該網格的空間</p></td></tr><tr><td valign="top"><p align="center">Android:layout_row</p></td><td valign="top"><p align="center">?</p></td><td valign="top"><p align="center">設置該子組件在GridLayout的第幾行</p></td></tr><tr><td valign="top"><p align="center">Android:layout_rowSpan</p></td><td valign="top"><p align="center">?</p></td><td valign="top"><p align="center">設置該子組件在GridLayout縱向上跨幾行</p></td></tr></tbody></table> ???????????????? ??????? 下面以計算器為例來講解GridLayout的使用: ~~~ <?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:rowCount="6" android:background="#000" android:columnCount="4"> <!--定義一個橫跨4列的文本框--> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_columnSpan="4" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:padding="5dp" android:layout_gravity="right" android:background="#EEEEEE" android:textColor="#000" android:text="0" android:singleLine="true" android:textSize="50sp"/> <!--定義一個橫跨4列的按鈕--> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_columnSpan="4" android:text="清除" android:textSize="22sp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="7" android:textSize="20sp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="8" android:textSize="20sp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="9" android:textSize="20sp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="/" android:layout_gravity="fill" android:textSize="20sp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="4" android:textSize="20sp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="5" android:textSize="20sp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="6" android:textSize="20sp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="*" android:layout_gravity="fill" android:textSize="20sp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" android:textSize="20sp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2" android:textSize="20sp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3" android:textSize="20sp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-" android:layout_gravity="fill" android:textSize="20sp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="." android:textSize="20sp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" android:textSize="20sp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="=" android:textSize="20sp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+" android:layout_gravity="fill_horizontal" android:textSize="20sp"/> </GridLayout> ~~~ ? ?? 效果圖如下: ![](https://box.kancloud.cn/2016-04-26_571f42c826e7d.jpg) 4. 幀布局(FramLayout) 幀布局為為每個加入其中的組件創建一個空白的區域,稱為一幀,每個子組件各占據一幀,然而這些組件都會根據grivaty屬性執行自動對齊。其實可以理解為堆撲克牌一樣,一張一張的堆在一起,上面的一張卡片放在下面一張卡片上面。 FrameLayout的常用屬性及相關方法 <table border="1" cellspacing="0" cellpadding="0"><tbody><tr><td valign="top"><p align="center">Xml屬性</p></td><td valign="top"><p align="center">相關方法</p></td><td valign="top"><p align="center">說明</p></td></tr><tr><td valign="top"><p align="center">Android:foreground</p></td><td valign="top"><p align="center">setForeground(Drawable)</p></td><td valign="top"><p align="center">設置該幀布局容器的前景圖像</p></td></tr><tr><td valign="top"><p align="center">Android:foregroundGravity</p></td><td valign="top"><p align="center">setForegroundGravity(int)</p></td><td valign="top"><p align="center">定義繪制前景圖像的gravity屬性</p></td></tr></tbody></table> ~~~ <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:width="300dp" android:height="300dp" android:background="#f00"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:width="200dp" android:height="200dp" android:background="#0f0"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:width="100dp" android:height="100dp" android:background="#00f"/> </FrameLayout> ~~~ 效果圖如下: ![](https://box.kancloud.cn/2016-04-26_571f42c83b57a.jpg) **5. 絕對布局(AbsoluteLayout)** 絕對布局通過直接設置組件的X? 、Y坐標來設置組件的位置,然而布局管理器將不會管理子組件的位置和大小,完全有開發人員設置。但是,使用絕對布局來控制子組件的大小是一個不太推薦的思路,因為市面上運行Android系統的手機差距相差萬別,它們的屏幕大小、分辨率都很不同,使用絕對布局是不可能做到各種屏幕上面顯示效果都一樣。因此AbsoluteLayout不覺管理器已經不適合在項目中使用了。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看