亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

【android基礎學習之八】——頁面布局

系統 2401 0

聲明:學習的書籍《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下。

實例效果:

【android基礎學習之八】——頁面布局

三、 界面布局之表單布局(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>
  

實例效果:

【android基礎學習之八】——頁面布局

四、界面布局之 切換卡(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();
            }            
        });
	}

  

實例效果:

【android基礎學習之八】——頁面布局 【android基礎學習之八】——頁面布局

好吧,基礎的控件與布局學完,下面開始新的學習。到學習P120頁

【android基礎學習之八】——頁面布局


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 亚洲美女在线观看播放 | 国产91精品系列在线观看 | 中文字幕一区二区三区精彩视频 | 精品小视频在线观看 | 操美女的视频网站 | 日韩一区二区不卡中文字幕 | 久久影院一区二区三区 | 免费看成人毛片日本久久 | 日本欧美强乱视频在线 | 四虎综合九九色九九综合色 | 911视频免费版 | 99久久精品国产一区二区 | 激情福利| 日韩一级黄色影片 | 欧美九九| 亚洲日本人成网站在线观看 | 久久澳门 | 99国产在线播放 | 欧美成人一区二区三区不卡 | 日本一级大毛片a一 | 99久久免费精品高清特色大片 | 欧美乱妇高清无乱码视频在线 | 成人日b视频 | 五月婷婷在线视频 | 日韩 欧美 国产 亚洲 中文 | 欧美精品国产一区二区三区 | 日韩一区二区视频在线观看 | 久久草在线观看 | 涩涩伊人| 99re6这里有精品热视频在线 | 亚洲欧美另类国产 | 四虎在线观看视频 | 日韩毛片在线影视 | 国产成人亚洲精品乱码在线观看 | 一区二区中文字幕亚洲精品 | 四虎影视在线看免费 720p | 四虎精品影院2022 | 97精品视频 | 国产va免费高清在线观看 | 色猫咪av在线网址 | 亚洲观看视频 |