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

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