日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

【android基礎(chǔ)學(xué)習(xí)之八】——頁面布局

系統(tǒng) 2839 0

聲明:學(xué)習(xí)的書籍《Android應(yīng)用開發(fā)揭秘》,這里記錄學(xué)習(xí)該書籍的日志,引用的相關(guān)代碼與總結(jié)描述,沒有商業(yè)的用途,完全是自我學(xué)習(xí)的一個記錄,剛剛學(xué)習(xí)不可避免會出現(xiàn)很多問題,若是有錯誤還請大家多多批評。

2011-10-31晚,完成最后一篇Android的基礎(chǔ)學(xué)習(xí),關(guān)于界面一些常用布局;

一、 界面布局之線性布局(LinearLayout)

之前的例子的學(xué)習(xí)已經(jīng)多次使用到了LinearLayout這個布局控件,線性布局分為:

(1)、垂直線性布局;

(2)、水平線性布局;

針對這兩種區(qū)別,只是一個屬性的區(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" 即可。

二、 界面布局之相對布局(RelativeLayout)

一般線性布局滿足不了們實(shí)際項(xiàng)目中的需要,就是一般做Web界面的UI設(shè)計(jì)一樣,也是存在相對元素的一些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="請輸入:"/>
    <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í)例效果:

【android基礎(chǔ)學(xué)習(xí)之八】——頁面布局

三、 界面布局之表單布局(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="導(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í)例效果:

【android基礎(chǔ)學(xué)習(xí)之八】——頁面布局

四、界面布局之 切換卡(TabWidget)

切換卡經(jīng)常用在一下選項(xiàng)上,類似于電話簿界面,通過多個標(biāo)簽切換顯示不同內(nèi)容。而其中,TabHost是一個用來存放Tab標(biāo)簽的容器,通過getTabHost方法來獲取TabHost的對象,通過addTab方法向容器里添加Tab。Tab在切換時都會產(chǎn)生一個事件,可以通過TabActivity的事件監(jiān)聽setOnTabChangedListener.

【擴(kuò)展點(diǎn)】TabHost

類概述

提供選項(xiàng)卡(Tab頁)的窗口視圖容器。此對象包含兩個子對象:一組是用戶可以選擇指定Tab頁的標(biāo)簽;另一組是FrameLayout用來顯示該Tab頁的內(nèi)容。個別元素通常控制使用這個容器對象,而不是設(shè)置在子元素本身的值。

(譯者madgoat注:即使使用的是單個元素,也最好把它放到容器對象ViewGroup里)

內(nèi)部類

interface TabHost.OnTabChangeListener

接口定義了當(dāng)選項(xiàng)卡更改時被調(diào)用的回調(diào)函數(shù)

interface TabHost.TabContentFactory

當(dāng)某一選項(xiàng)卡被選中時生成選項(xiàng)卡的內(nèi)容

class TabHost.TabSpec

單獨(dú)的選項(xiàng)卡,每個選項(xiàng)卡都有一個選項(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>

  

處理類:

    //聲明TabHost對象
	TabHost mTabHost;
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		//取得TabHost對象
		mTabHost = getTabHost();
	    
		/* 為TabHost添加標(biāo)簽 */
		//新建一個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)前顯示哪一個標(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í)例效果:

【android基礎(chǔ)學(xué)習(xí)之八】——頁面布局 【android基礎(chǔ)學(xué)習(xí)之八】——頁面布局

好吧,基礎(chǔ)的控件與布局學(xué)完,下面開始新的學(xué)習(xí)。到學(xué)習(xí)P120頁

【android基礎(chǔ)學(xué)習(xí)之八】——頁面布局


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 汶上县| 临桂县| 招远市| 鲁山县| 海丰县| 瑞金市| 钟祥市| 伊川县| 伊春市| 柯坪县| 邻水| 车险| 金华市| 樟树市| 景宁| 上蔡县| 都江堰市| 浏阳市| 乡城县| 吴江市| 阜宁县| 怀远县| 赤水市| 呈贡县| 合水县| 榆中县| 景宁| 开平市| 舒城县| 东光县| 修文县| 长丰县| 浙江省| 香港 | 金溪县| 财经| 安化县| 宁城县| 九龙城区| 双峰县| 禄劝|