聲明:學(xué)習(xí)的書(shū)籍《Android應(yīng)用開(kāi)發(fā)揭秘》,這里記錄學(xué)習(xí)該書(shū)籍的日志,引用的相關(guān)代碼與總結(jié)描述,沒(méi)有商業(yè)的用途,完全是自我學(xué)習(xí)的一個(gè)記錄,剛剛學(xué)習(xí)不可避免會(huì)出現(xiàn)很多問(wèn)題,若是有錯(cuò)誤還請(qǐng)大家多多批評(píng)。
2011-10-31晚,完成最后一篇Android的基礎(chǔ)學(xué)習(xí),關(guān)于界面一些常用布局;
一、 界面布局之線性布局(LinearLayout)
之前的例子的學(xué)習(xí)已經(jīng)多次使用到了LinearLayout這個(gè)布局控件,線性布局分為:
(1)、垂直線性布局;
(2)、水平線性布局;
針對(duì)這兩種區(qū)別,只是一個(gè)屬性的區(qū)別
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> </LinearLayout>
水平線性布局的話,android:orientation="horizontal" 即可。
二、 界面布局之相對(duì)布局(RelativeLayout)
一般線性布局滿足不了們實(shí)際項(xiàng)目中的需要,就是一般做Web界面的UI設(shè)計(jì)一樣,也是存在相對(duì)元素的一些CSS樣式的布局。RelativeLayout參數(shù)有:Width,Height,Below,AlignTop,ToLeft,Padding,和MerginLeft。
關(guān)鍵源碼:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/label" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="請(qǐng)輸入:"/> <EditText android:id="@+id/entry" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@android:drawable/editbox_background" android:layout_below="@id/label"/> <Button android: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="確定" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/ok" android:layout_alignTop="@id/ok" android:text="取消" /> </RelativeLayout>
其中,android:layout_below=”@id/label”設(shè)置了EditText處于TextView下方;在Button中android:layout_below=”@id/entry”設(shè)置該Button位于EditText下。
實(shí)例效果:
三、 界面布局之表單布局(TableLayout)
TableLayout由許多TableRow組成,每個(gè)TableRow都會(huì)定義一個(gè)Row。TableLayout容器不會(huì)顯示Row,Column或Cell的邊框線,每個(gè)Row擁有0個(gè)或多個(gè)Cell,每個(gè)Cell擁有一個(gè)View對(duì)象。表格由行和列組成許多單元個(gè),允許單元格為空。但是單元格不能跨列,這與Html不同。
<View android:layout_height="2dip" android:background="#FF909090" /> <TableRow> <TextView android:text="*" android:padding="3dip" /> <TextView android:text="導(dǎo)入..." android:padding="3dip" /> </TableRow> <TableRow> <TextView android:text="*" android:padding="3dip" /> <TextView android:text="導(dǎo)出..." android:padding="3dip" /> <TextView android:text="Ctrl-E" android:gravity="right" android:padding="3dip" /> </TableRow>
實(shí)例效果:
四、界面布局之 切換卡(TabWidget)
切換卡經(jīng)常用在一下選項(xiàng)上,類(lèi)似于電話簿界面,通過(guò)多個(gè)標(biāo)簽切換顯示不同內(nèi)容。而其中,TabHost是一個(gè)用來(lái)存放Tab標(biāo)簽的容器,通過(guò)getTabHost方法來(lái)獲取TabHost的對(duì)象,通過(guò)addTab方法向容器里添加Tab。Tab在切換時(shí)都會(huì)產(chǎn)生一個(gè)事件,可以通過(guò)TabActivity的事件監(jiān)聽(tīng)setOnTabChangedListener.
【擴(kuò)展點(diǎn)】TabHost
類(lèi)概述
提供選項(xiàng)卡(Tab頁(yè))的窗口視圖容器。此對(duì)象包含兩個(gè)子對(duì)象:一組是用戶可以選擇指定Tab頁(yè)的標(biāo)簽;另一組是FrameLayout用來(lái)顯示該Tab頁(yè)的內(nèi)容。個(gè)別元素通常控制使用這個(gè)容器對(duì)象,而不是設(shè)置在子元素本身的值。
(譯者madgoat注:即使使用的是單個(gè)元素,也最好把它放到容器對(duì)象ViewGroup里)
內(nèi)部類(lèi)
interface TabHost.OnTabChangeListener
接口定義了當(dāng)選項(xiàng)卡更改時(shí)被調(diào)用的回調(diào)函數(shù)
interface TabHost.TabContentFactory
當(dāng)某一選項(xiàng)卡被選中時(shí)生成選項(xiàng)卡的內(nèi)容
class TabHost.TabSpec
單獨(dú)的選項(xiàng)卡,每個(gè)選項(xiàng)卡都有一個(gè)選項(xiàng)卡指示符,內(nèi)容和tag標(biāo)簽,以便于記錄.。
關(guān)鍵源碼:
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/textview1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="this is a tab" /> <TextView android:id="@+id/textview2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="this is another tab" /> <TextView android:id="@+id/textview3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="this is a third tab" /> </FrameLayout> </LinearLayout> </TabHost>
處理類(lèi):
//聲明TabHost對(duì)象 TabHost mTabHost; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //取得TabHost對(duì)象 mTabHost = getTabHost(); /* 為T(mén)abHost添加標(biāo)簽 */ //新建一個(gè)newTabSpec(newTabSpec) //設(shè)置其標(biāo)簽和圖標(biāo)(setIndicator) //設(shè)置內(nèi)容(setContent) mTabHost.addTab(mTabHost.newTabSpec("tab_test1") .setIndicator("TAB 1",getResources().getDrawable(R.drawable.img1)) .setContent(R.id.textview1)); mTabHost.addTab(mTabHost.newTabSpec("tab_test2") .setIndicator("TAB 2",getResources().getDrawable(R.drawable.img2)) .setContent(R.id.textview2)); mTabHost.addTab(mTabHost.newTabSpec("tab_test3") .setIndicator("TAB 3",getResources().getDrawable(R.drawable.img3)) .setContent(R.id.textview3)); //設(shè)置TabHost的背景顏色 mTabHost.setBackgroundColor(Color.argb(150, 22, 70, 150)); //設(shè)置TabHost的背景圖片資源 //mTabHost.setBackgroundResource(R.drawable.bg0); //設(shè)置當(dāng)前顯示哪一個(gè)標(biāo)簽 mTabHost.setCurrentTab(0); //標(biāo)簽切換事件處理,setOnTabChangedListener mTabHost.setOnTabChangedListener(new OnTabChangeListener(){ public void onTabChanged(String tabId) { Dialog dialog = new AlertDialog.Builder(Examples_04_29Activity.this) .setTitle("提示") .setMessage("當(dāng)前選中:"+tabId+"標(biāo)簽") .setPositiveButton("確定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton){ dialog.cancel(); } }).create();//創(chuàng)建按鈕 dialog.show(); } }); }
實(shí)例效果:
好吧,基礎(chǔ)的控件與布局學(xué)完,下面開(kāi)始新的學(xué)習(xí)。到學(xué)習(xí)P120頁(yè)
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
