《Android實(shí)戰(zhàn)基礎(chǔ)教程》課件第二章_第1頁(yè)
《Android實(shí)戰(zhàn)基礎(chǔ)教程》課件第二章_第2頁(yè)
《Android實(shí)戰(zhàn)基礎(chǔ)教程》課件第二章_第3頁(yè)
《Android實(shí)戰(zhàn)基礎(chǔ)教程》課件第二章_第4頁(yè)
《Android實(shí)戰(zhàn)基礎(chǔ)教程》課件第二章_第5頁(yè)
已閱讀5頁(yè),還剩33頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

目錄2.1線性布局2.2相對(duì)布局

2.3表格布局

2.4幀布局2.5Android常見(jiàn)顯示單位布局二、布局

線性布局分為垂直線性布局和水平線性布局。LinearLayout是線性布局控件,它包含的子控件將以橫向或豎向的方式按照相對(duì)位置來(lái)排列所有的widgets或者其他的containers,超過(guò)邊界時(shí),某些控件將缺失或消失。因此一個(gè)垂直列表的每一行只會(huì)有一個(gè)widget或者是container,而不管他們有多寬;而一個(gè)水平列表將會(huì)只有一個(gè)行高(高度為最高子控件的高度加上邊框高度)。LinearLayout保持其所包含的widgets或者是containers之間的間隔以及對(duì)齊方式(相對(duì)一個(gè)控件的右對(duì)齊、中間對(duì)齊或者左對(duì)齊)。2.1線性布局二、布局

LinearLayout屬性如下:android:orientation:定義布局的方向——水平或垂直。android:layout_weight:子元素對(duì)未占用空間【水平或垂直】分配權(quán)重值,其值越小,權(quán)重越大。android:layout_width:寬度(fill_parent:父元素決定,wrap_content:本身的內(nèi)容決定)。android:layout_height:高度(直接指定一個(gè)px值)。android:gravity:內(nèi)容的排列形式(常用

top,bottom,left,right,center)。二、布局

根據(jù)一個(gè)實(shí)例來(lái)了解線性布局。

新建項(xiàng)目MyLayout,在activity_main中添加4個(gè)按鈕,代碼如下:activity_main.xml文件:代碼MainActivity文件:代碼

啟動(dòng)模擬器,運(yùn)行結(jié)果如圖2.1所示。圖2.1二、布局

RelativeLayout稱為相對(duì)布局,可以通過(guò)相對(duì)的定位方式讓控件出現(xiàn)在布局的任何位置。RelativeLayout的屬性:Android:ignoregravity:設(shè)置哪個(gè)組件不受gravity屬性的影響。

2.2相對(duì)布局二、布局1)屬性值為具體像素值的屬性android:layout_marginBottom:離某元素底邊緣的距離。android:layout_marginLeft:離某元素左邊緣的距離。android:layout_marginRight:離某元素右邊緣的距離。android:layout_marginTop:離某元素上邊緣的距離。二、布局2)屬性值為true或是false的屬性android:layout_alignParentBottom:控制該組件是否和布局管理器底端對(duì)齊。android:layout_alignParentLeft:控制該組件是否和布局管理器左邊對(duì)齊。android:layout_alignParentRight:控制該組件是否和布局管理器右邊對(duì)齊。android:layout_alignParentTop:控制該組件是否和布局管理器頂部對(duì)齊。二、布局

3)屬性值為其他組件ID的屬性android:layout_toLeftOf:本組件在某組件的左邊。android:layout_toRightOf:本組件在某組件的右邊。android:layout_above:本組件在某組件的上方。android:layout_below:本組件在某組件的下方。二、布局

以一個(gè)項(xiàng)目為例來(lái)講述相對(duì)布局的屬性。

創(chuàng)建項(xiàng)目MyRelativeLayout,修改activity_main.xml的代碼(添加五個(gè)按鈕控件)。activity_main.xml文件:代碼

MainActivity文件:代碼

運(yùn)行模擬器,結(jié)果如圖2.2所示。圖2.2二、布局

表格布局(TableLayout)即排成行和列的布局。一個(gè)TableLayout由若干TableRow組成,TableRow定義了一行。TableLayout容器不顯示行、列或單元格邊框線。每行有零個(gè)或多個(gè)單元格,每個(gè)單元格可容納一個(gè)視圖對(duì)象。

列的寬度是由該列中最寬的單元格決定的,通過(guò)調(diào)用setcolumnshrinkable()或setcolumnstretchable(),一個(gè)TableLayout可以指定某些列為收縮或伸展的。2.3表格布局二、布局TableLayout的子類不能指定寬度屬性,寬度總是match_parent。而layout_height屬性可以通過(guò)它的子控件來(lái)設(shè)置,默認(rèn)值是wrap_content。如果子控件是個(gè)TableRow,那么它的高度總是wrap_content。

必須將單元格添加到遞增列順序中,無(wú)論是在代碼或XML中。默認(rèn)列數(shù)為零。如果你不指定一個(gè)孩子單元格列數(shù),它將自動(dòng)遞增到下一個(gè)可用的列。如果你跳過(guò)了一個(gè)列號(hào),它就會(huì)被認(rèn)為是一個(gè)空單元格。二、布局。屬性名相關(guān)方法描

述android:collapseColumnssetColumnCollapsed(int,boolean)設(shè)置需要隱藏的列android:shrinkColumnssetShrinkAllColumns(boolean)設(shè)置允許收縮的列android:stretchColumnssetStretchAllColumns(boolean)設(shè)置允許拉伸的列表2.1表格布局的XML屬性二、布局表格布局案例:布局文件:activity_main.xml文件:代碼使用Activity顯示該布局,得到如圖2.3所示效果。圖2.3二、布局

幀布局是指在屏幕的一塊區(qū)域中顯示單獨(dú)的一個(gè)元素。幀布局是最簡(jiǎn)單的布局形式。所有添加到這個(gè)布局中的視圖都以層疊的方式顯示。第一個(gè)添加的控件被放在最底層,最后一個(gè)添加到框架布局中的視圖顯示在最頂層,上一層的控件會(huì)覆蓋下一層的控件。這種顯示方式有些類似于堆棧。

2.4幀布局二、布局

當(dāng)我們往幀布局中添加組件的時(shí)候,所有的組件都會(huì)放置于這塊區(qū)域的左上角;幀布局的大小由子控件中最大的子控件決定;如果組件大小相同,同一時(shí)刻就只能看到最上面的那個(gè)組件。當(dāng)然我們也可以為組件添加layout_gravity屬性,從而制定組件的對(duì)齊方式。屬性名相關(guān)方法描

述android:foregroundsetForeground(Drawable)設(shè)置前景圖像android:foregroundGravitysetForegroundGravity(int)定義繪制前景圖像的gravity屬性android:measureAllChildrensetMeasureAllChildren(boolean)決定是否測(cè)量所有子類或只是那些在可見(jiàn)或不可見(jiàn)狀態(tài)時(shí)測(cè)量表2.2幀布局的XML屬性二、布局幀布局案例:布局文件:activity_main.xml文件:代碼使用FrameLayout布局,得到如圖2.4所示效果。圖2.4二、布局1.像素px(pixels)px是像素的意思,即屏幕中可以顯示的最小元素單位。Android應(yīng)用程序中任何可見(jiàn)的圖像都是由一個(gè)個(gè)像素點(diǎn)組成的。目前HVGA代表320×480像素。

一個(gè)組件的不同像素,在分辨率不同的手機(jī)上顯示的效果是不同的。低分辨率的手機(jī),圖像看起來(lái)比較大;高分辨率手機(jī),圖像看起來(lái)比較小,用戶體驗(yàn)不同。

2.5Android常見(jiàn)顯示單位二、布局2.設(shè)備獨(dú)立像素dip或dp(deviceindependentpixels)dip和設(shè)備硬件有關(guān),不依賴像素。一般為了支持WVGA、HVGA和QVGA,推薦使用這種顯示方式。

這里要特別注意,dip與屏幕密度有關(guān),而屏幕密度又與具體的硬件有關(guān),硬件設(shè)置不正確,有可能導(dǎo)致dip不能正常顯示。在屏幕密度為160的顯示屏上,1dip=1px,如果屏幕分辨率很高,如480×800,但是屏幕密度沒(méi)有正確設(shè)置,比如說(shuō)還是160,那么此時(shí)凡是使用dip的都會(huì)顯示異常,普遍表現(xiàn)為顯示過(guò)小。dip的換算:dip(value)=(int)(px(value)/1.5+0.5)二、布局3.比例像素sp(scaledpixels—bestfortextsize)sp主要處理字體的大小,用于字體顯示,可以根據(jù)系統(tǒng)的字體自適應(yīng)。

除了上面三個(gè)顯示單位,還有如下幾種不太常用的顯示單位:

in(inches)英寸

mm(millimeters)毫米

pt(points)點(diǎn),1/72英寸另外根據(jù)google的推薦,像素統(tǒng)一使用dip,字體統(tǒng)一使用sp。二、布局<LinearLayoutxmlns:android="/apk/res/android" xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!--按鈕1--> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button1" android:text="button1"/>二、布局 <!--按鈕2--> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button2" android:text="button2"/> <!--按鈕3--> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button3" android:text="button3"/>二、布局 <!--按鈕4--> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button4" android:text="button4"/></LinearLayout>back二、布局publicclassMainActivityextendsActivity{

@Override protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}}back二、布局<RelativeLayoutxmlns:android="/apk/res/android" xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <!--按鈕1--> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:id="@+id/button1" android:text="button1"/>二、布局 <!--按鈕2--> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button2" android:text="button2" android:layout_above="@+id/button1" android:layout_toLeftOf="@+id/button1"/>二、布局 <!--按鈕3--> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button3" android:text="button3" android:layout_below="@+id/button1" android:layout_toLeftOf="@+id/button1"/>二、布局 <!--按鈕4--> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button4" android:text="button4" android:layout_above="@+id/button1" android:layout_toRightOf="@+id/button1"/>二、布局 <!--按鈕5--> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button5" android:text="button5" android:layout_below="@+id/button1" android:layout_toRightOf="@+id/button1"/></RelativeLayout>back二、布局publicclassMainActivityextendsActivity{ @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }}back二、布局<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">二、布局 <!--表格1-伸展--> <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:shrinkColumns="0,1,2"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="一行"/> <TableRow>二、布局 <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕1"> </Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕2"> </Button>二、布局

<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕2"> </Button> </TableRow> <TableRow> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕4"> </Button>二、布局 <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_span="2" android:text="兩列"> </Button> </TableRow> </TableLayout>二、布局 <!--表格2-收縮--> <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:stretchColumns="0,1"> <TableRow> <Button android:layout_width="wrap_content" android:layout_height="wrap_content"

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論