月22日-課件布局管理器_第1頁
月22日-課件布局管理器_第2頁
月22日-課件布局管理器_第3頁
月22日-課件布局管理器_第4頁
月22日-課件布局管理器_第5頁
已閱讀5頁,還剩24頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

界面編程與視圖View第一節(jié)2視圖與容器組件用戶界面Android提供了大量功能豐富的UI組件,只要按照一定的規(guī)律把這些UI組件組合起來,就可以開發(fā)出優(yōu)秀的圖形用戶界面。Android應(yīng)用的大部分組件都放在android.widget包及子包、android.view包及其子包當(dāng)中。所有的UI組件都繼承了View類,View類有一個很重要的子類ViewGroup類。開發(fā)者可以對View和ViewGroup進(jìn)行組合,來完成應(yīng)用程序界面設(shè)計(jì)。3ViewsViewView是Android中圖形界面的基類,提供了可視化界面的展示。Android的圖形界面可分為三層:底層是

Activity;Activity上面是Window;Window上面是View。任何一個View對象都繼承android.view.View類。它是一個 有屏幕上特定的一個矩形布局和內(nèi)容屬性的數(shù)據(jù)結(jié)構(gòu)。作為一個基類,View類為Widget服務(wù),Widget則是一組用于繪制交互屏幕元素的完全實(shí)現(xiàn)子類。45ViewGroupViewGroupView分為View和ViewGroup。View是基本的控件;ViewGroup是布局控件。ViewGroup是一個android.view.ViewGroup類的對象。它的功能是裝載和管理一組下層的View和其他的ViewGroup,作為一個基類,ViewGroup為Layout服務(wù)。6ViewGroupViewGroup容器控制其子組件的分布依賴于

ViewGroup.LayoutParams、ViewGroup.MarginLayoutParamsXML屬性相關(guān)方法說明android:layout_marginBottomsetMargins(int,int,int,int)指定該子組件下邊的頁邊距android:layout_marginLeftsetMargins(int,int,int,int)指定該子組件左邊的頁邊距android:layout_marginRightsetMargins(int,int,int,int)指定該子組件右邊的頁邊距android:layout_marginTopsetMargins(int,int,int,int)指定該子組件上邊的頁邊距Android幫助文檔Android幫助文檔對于每個Android開發(fā)者而言,的。Android提供的 文檔是必看在Android

SDK安裝 下找到docs子

,打開docs子index.html頁面,并單擊該頁面上方的Dev

Guide

面,這就是Android

提供的開發(fā)指南文檔。接著單擊下圖中的Reference頁,看到的就是Android的API文檔了。78控制組件方式控制組件行為的方式Android控制組件行為的方式有以下三種:在XML布局文件中通過XML屬性進(jìn)行控制在Java程序代碼中進(jìn)行控制XML布局文件和Java代碼混合控制XML布局文件控制XML布局文件中進(jìn)行控制在Android應(yīng)用的res/layout

下定義一個文件名任意的XML布局文件之后,Java代碼通過如下方法在Activity中顯示該視圖:setContentView(R.layout.<資源文件名字>);當(dāng)在布局文件中添加多個UI組件時,UI組件通過android:id屬性設(shè)置,如:android:id=“@+id/<組件ID>”該屬性屬性值代表該組件的唯一標(biāo)識??赏ㄟ^以下方法在Java代碼中 該UI組件:findViewById(R.id.<android.id屬性值>);910控在p

kdbn.setOnClickListener(new

OnClickListener(){@Overridepublic

void

onClick(View

v){show.setText(" o

,Android

,

"+

new

java.util.Date());}});}}layout.setOrientation(LinearLayout.VERTICAL);//創(chuàng)建一個TextViewfinal

TextView

show

=

new

TextView(this);//創(chuàng)建一個按鈕Button

bn

=

new

Button(this);bn.setText(R.string.ok);bn.setLayoutParams(new

ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));//向Layout容器中添加TextViewlayout.addView(show);//向Layout容器中添加按鈕layout.addView(bn);//為按鈕綁定一個事件

器11把變package

com.gec.mixview;混//初始化時顯示第一張image

setImageResource(images[0]);<?xml

version="1.0"encoding="utf-8"?><!--定義一個線性布局容器--><LinearLayout

xmlns:android="http://s

/apk/res/android"android:id="@+id/root"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"></LinearLayout>//改變ImageView里顯示的

image.setImageResource(images[++currentImg]);}});}});y

(

y

)

y

(//程序創(chuàng)建ImageView組件final

ImageView

image

=

new

ImageView(this);//將ImageView組件添加到LinearLayout布局容器中

main.addView(image);//初始化時顯示第一張image

setImageResource(images[0]);12自定義View自定義View當(dāng)混合使用XML布局文件和代碼來控制UI界面時,上把變化小、行為比較固定的組件放在XML布局管理中,而把那些變化較多、行為控制比較復(fù)雜的組件交給Java代碼來管理。開發(fā)自定義View基于Android

UI組件的實(shí)現(xiàn)原理,開發(fā)者完全可以開發(fā)出項(xiàng)目定制的組件,當(dāng)Android系統(tǒng)提供的UI組件不足以滿足項(xiàng)目需要時,開發(fā)者可以繼承View來派生自定義組件。Vpublic

class

CustomView

extends

Activity{@Overridepublic

voidonCreate(Bundle

savedInstanceState)<?xml

version="1.0"

encoding="utf-8"?><LinearLayoutxmlns:android="http://s

/apk/res/

android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"android:id="@+id/root"></LinearLayout>draw.currentY=

event.getY();//通知draw組件重繪draw.invalidate();//返回true表明處理方法已經(jīng)處理該事件return

true;}});root.addView(draw);}14布局管理器第二節(jié)布局管理器Android最大的優(yōu)點(diǎn)就是代碼(java)和布局(xml)分離。Layout就是負(fù)責(zé)管理控件在屏幕的位置的類,下面是android

SDK已經(jīng)內(nèi)置的簡單幾種布局模型:1、LinearLayout(線性布局)2、RelativeLayout(相對布局)3、TableLayout(表格布局)4、FrameLayout(框架布局)5、Absolu

ayout(絕對布局)15布局管理器類圖167aa_android:layout_width="wrap_content"o

android:layout_height="wrap_content"android:text="@string/bn2"/><Buttonandroid:id="@+id/bn3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/bn3"/>線/><Buttonandroid:id="@+id/bn4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/bn4"/><Buttonandroid:id="@+id/bn5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/bn5"/></LinearLayout>android:id=@+id/bn218線性布局屬性線性布局組件常用屬性android:id

——為控件指定相應(yīng)的IDandroid:text——指定控件當(dāng)中顯示的文字,需要注意的是,這里盡量使用strings.xml文件當(dāng)中的字符串a(chǎn)ndroid:gravity——指定控件的基本位置,比如說居中,居右等位置

android:textSize——指定控件當(dāng)中字體的大小

android:background——指定該控件所使用的背景色,RGB命名法android:width——指定控件的寬度android:height——指定控件的高度android:padding——指定控件的內(nèi)邊距,也就是說控件當(dāng)中的內(nèi)容android:singleLine——如果設(shè)置為真的話,則將控件的內(nèi)容在同一行當(dāng)中進(jìn)行顯示android:layout_weight:占據(jù)相對屏幕的大小。表格布局TableLayout

表格布局TableLayout是比較常用的布局,采用行和列的形式來管理UI組件。并不需要明確 行和列,而是通過添加TableRow、其他組件來控制表格的行數(shù)和列數(shù)。每次向TableLayout中添加TableRow,該TableRow就是一個表格行,TableRow也是容器,也可以向其中添組件,每添加一個子組件該表格就增加一列。如果直接向TableLayout中添加組件,那么該組件直接占一行。在表格布局中,列的寬度由該列中最寬的那個單元格決定。整個表格布局的寬度取決于父容器的寬度。190表隔擠出<?xml

version="1.0"

encoding="utf-8"?><LinearLayout

xmlns:android="http://s

/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><!--定義第一個表格布局,指定第2列允許收縮,第3列允許拉伸--><TableLayout

android:id="@+id/TableLayout01"android:layout_width="fill_parent"android:layout_height="wrap_content"android:shrinkColumns="1"android:stretchColumns="2"><!--直接添加按鈕,它自己會占一行--><Button

android:id="@+id/ok1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="獨(dú)自一行的按鈕"/><!--添加一個表格行--><TableRow><!--為該表格行添加3個按鈕--><Button

android:id="@+id/ok2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="普通按鈕"/>21表格布局實(shí)驗(yàn)L用Table2<?xml

version="1.0"encoding="utf-8"?><FrameLayout

xmlns:android="http://s

/apk/res/android"幀android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:layout_width="300dip"android:layout_height="300dip"android:background="#00008B"android:layout_gravity="center"/><TextViewandroid:layout_width="250dip"android:layout_height="250dip"android:background="#0000CD"android:layout_gravity="center"/><TextViewandroid:layout_width="200dip"android:layout_height="200dip"android:background="#0000FF"android:layout_gra/>vity="center"<TextViewandroid:layout_width="150dip"android:layout_height="150dip"幀布局實(shí)驗(yàn)實(shí)現(xiàn)一個霓虹燈效果23相對布局RelativeLayout相對布局相對布局(RelativeLayout),在容器的子元素們可以使用彼此之間的相對位置或者和容器間的相對位置來進(jìn)行定位。間產(chǎn)生循注意,不能在RelativeLayout容器本身和他的子元環(huán)依賴,比如說,不能將RelativeLayout的高設(shè)置成為WRAP_CONTENT的時候?qū)⒆釉氐母咴O(shè)置成為ALIGN_PARENT_BOTTOM。XML屬性相關(guān)方法說明android:ignoreGravitysetIgnoreGravity(int)設(shè)置該布局容器內(nèi)哪個子元素會不受Gravity的影響.android:gravitysetGravity(int)設(shè)置該布局容器

各子組件的對齊方式24相對布局屬性一相對布局屬性android:layout_alignParentBottom如果該值為true,則將該控件的底部和父控件的底部對齊android:layout_alignParentLeft

如果該值為true,則將該控件的左邊與父控件的左邊對齊android:layout_alignParentRight

如果該值為true,則將該控件的右邊與父控件的右邊對齊android:layout_alignParentTop

如果該值為true,則將控件的頂部與父控件的頂部對齊android:layout_centerHorizontal

如果值為真,該控件將被至于水平方向的android:layout_centerInParent

如果值為真,該控件將被至于父控件水平方向和垂直方向的android:layout_centerVertical

如果值為真,該控件將被至于垂直方向的256部邊齊對齊對aaaaa緣aaa齊<?xml

version="1.0"

encoding="utf-8"?><RelativeLayout

xmlns:android="http://s

/apk/res/android"相android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:id="@+id/label"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="輸入"/><EditTextandroid:id="@+id/entry"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_below="@id/label"/><Buttonandroid:id="@+id/ok"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/entry"android:layout_alignParentRight="true"android:layout_marginLeft="10dip"android:text="確定"

/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toLeftOf="@id/ok"android:layout_alignTop="@id/ok"android:text="取消"/>27相對布局實(shí)驗(yàn)實(shí)現(xiàn)一個梅花布局效果8作<?xml

version="1.0"encoding="utf-8"?><Absolu

ayout

xmlns:android="http://s/apk/res/android"絕android:layout_width="f

溫馨提示

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

最新文檔

評論

0/150

提交評論