android開發學習筆記系列(2)-android應用介面程式設計

King_Alex發表於2015-03-09

前言

本篇部落格將會簡要介紹andriod開發過程中的一些介面元素和程式設計的實現,我將大家走進安卓的XML世界,當然可能會涉及到java程式碼,當然本文主要是介紹XML檔案的介面佈局。

那麼我們的XML存在哪裡呢?很簡單,就在資原始檔裡面,什麼?你不知道資原始檔是哪個?拜託,開啟專案檔案,看見res,那麼這個檔案低下就是我們的資原始檔了!哇哦,那麼我們在這上面程式設計嗎?NO,我基本上把邏輯都寫在src檔案下的.java檔案!

如何使用API文件

在開發的時候,我們最需要知道的就是如何使用API啦,有人老喜歡去百度啦,那麼告訴你,這樣不好!我們其實有現成的,你為何不用呢?

看,在你的SDK下載的資料夾下有docs資料夾,開啟它,直接點選index,那麼你就可以直接讀API幫助文件啦!

注意,我們的幫助文件是英文的,而且你會發現在你在上網的時候,你的幫助文件非常之卡頓,甚至載入不出來,那是因為它在向官網尋求請求,但是google被封,你懂的!這不是坑爹嗎?不用著急!英文不好,載入慢,OK,我給大家一個網址,國內的,而且已經對API全部進行了翻譯!

android中文API文件

快順手儲存這個網址吧!那麼我們是如何使用開發文件的呢?又是在哪裡找到這些API呢?在首頁中找到Develop->API Guides!這個是android開發的一些介紹,而Reference,全部都是相應的API檔案!

android開發學習筆記系列(2)-android應用介面程式設計

控制元件簡介

相信開發android的朋友都有這樣的感受,就是我們用XML來定製介面,用java來實現功能。但安卓總是給程式設計師們開發相應的空間,讓程式設計師們也可以通過程式碼來控制元件!在這裡,我就不一一列舉View類下的UI元件,但一定要知道這個既然有XML屬性,那麼一般會有java的相應方法!在這裡我也不舉例來使用java程式碼來設定相應的介面了,一般情況下,我們絕對不是使用這樣的模式來寫自己的應用的,如果您非要這要做的話,瘋狂的android講義上有大量這樣的例子~~~至於如何使用構造器來重新繪製View,我覺得實在有必要再寫一篇文章來論一論了!(呵呵,我又有新的文章可寫了!)

佈局管理

我記得最清楚的是:當時新建一個安卓的helloworld Project時,我的佈局模式就是相對佈局,那麼瞭解佈局的模式是必不可少的啦!

對了,對於新手的你,知道佈局檔案在什麼地方嗎?不錯,就在新建專案時產生activity_main.xml的地方,即res檔案下的layout檔案!

那麼我在這裡就不介紹我們最常用的線性佈局LinearLayout(相信每個入門的人都會直接使用線性佈局來寫demo),主要來介紹我們常用到的相對佈局。

幀佈局FrameLayout

幀佈局是一幀佈局的,是可以覆蓋的一個佈局,那麼我們在使用TabHost元件的時候必然會用到幀佈局的!那麼在這裡我就不詳細說明它的用法!大家可以參考別人的部落格!

談Android五大布局(一)——LinearLayout、FrameLayout和AbsoulteLayout

題外話:當你用線性佈局做好了demo,請多寫寫相對佈局的檔案,因為我感覺我在開發中最常用到的永遠是相對佈局!

相對佈局RelativeLayout

相對佈局是在開發過程中最常用到的開發模式了!下面是對相對佈局的是xml的屬性!

LayoutParams裡的屬性,只能賦予true或者flase

  • android:layout_centerHorizontal 控制子控制元件是否在容器中水平居中
  • android:layout_centerInParent 控制子控制元件是否在容器中居中
  • android:layout_centerVerical 控制子控制元件是否在容器中垂直居中

屬性賦值為ID的

  • android:layout_toRightOf 控制子控制元件位於ID控制元件的右邊
  • android:layout_toLeftOf 控制子控制元件位於ID控制元件的左邊
  • android:layout_above 控制子控制元件位於ID控制元件的上方
  • android:layout_below 控制子控制元件位於ID控制元件的下方
  • android:layout_alignTop 控制子控制元件位於ID控制元件自身的上邊界對齊
  • android:layout_alignBottom 控制子控制元件位於ID控制元件自身的下邊界對齊
  • android:layout_alignLeft 控制子控制元件位於ID控制元件自身的左邊界對齊
  • android:layout_alignRight 控制子控制元件位於ID控制元件自身的右邊界對齊

屬性為px

  • android:layout_marginLeft 控制子控制元件與容器左邊的距離
  • android:layout_paddingLeft 控制子控制元件與自身左邊的距離

    僅舉出2例子,其他也是和上面的一樣變化為Right、Top

元件

在元件中,經常會用到選單欄,那麼今天我們就介紹使用TabHost這個控制元件,其他的,比如TextView,Button等最常用的元件我就不介紹了,至於後面幾個控制元件,在後期的部落格中會一一介紹出來的!

TabHost元件

盛放Tab的容器就是TabHost。TabHost的實現有兩種方式:

  • 第一種繼承TabActivity,從TabActivity中用getTabHost()方法獲取TabHost。各個Tab中的內容在佈局檔案中定義就行了。
  • 第二種方式,不繼承TabActivity,在佈局檔案中定義TabHost即可,但是TabWidget的id必須是@android:id/tabs,FrameLayout的id必須是@android:id/tabcontent

    繼承TabActivity

    先來看一下佈局檔案,
 <?xml version="1.0" encoding="utf-8"?>
 <!-- 定義TabHost元件 -->
 <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent">
   <!-- 定義第一個標籤頁的內容 -->
   <LinearLayout android:id="@+id/tab01" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
     <!-- 定義兩個TextView用於顯示標籤頁中的內容 -->
     <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="你好"/> 
     <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="我好"/>
   </LinearLayout>
   <!-- 定義第二個標籤頁的內容 -->
   <LinearLayout android:id="@+id/tab02" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
     <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="大家好"/> 
     <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="你沒救了"/>
   </LinearLayout>
   <!-- 定義第三個標籤頁的內容 -->
   <LinearLayout android:id="@+id/tab03" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
     <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="真的嗎"/> 
     <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="呵呵"/>
   </LinearLayout>
 </TabHost>

java檔案

public class HelloTabHost extends TabActivity {

   @Override
   public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
 
     //呼叫TabActivity的getTabHost()方法獲取TabHost物件
     TabHost tabHost = getTabHost();
 
     //設定使用TabHost佈局
     LayoutInflater.from(this).inflate(R.layout.main, tabHost.getTabContentView(),true);
 
     //新增第一個標籤頁
     tabHost.addTab(tabHost.newTabSpec("tab01").setIndicator("題外話").setContent(R.id.tab01));
 
     //新增第二個標籤頁,並在其標籤上新增一個圖片
     tabHost.addTab(tabHost.newTabSpec("tab02").setIndicator("天涯",getResources().getDrawable(R.drawable.icon)).setContent(R.id.tab02));
 
     //新增第三個標籤頁
     tabHost.addTab(tabHost.newTabSpec("tab03").setIndicator("無芳草").setContent(R.id.tab03));
   }
 }

直接找TabHost的ID實現

xml檔案:

<?xml version="1.0" encoding="utf-8"?>  
  <!-- TabHost必須包含一個 TabWidget和一個FrameLayout-->   
  <TabHost android:id="@+id/tabhost" android:layout_width="fill_parent" android:layout_height="wrap_content">  
        <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">  
            <!-- TabWidget的id屬性必須為 @android:id/tabs-->              
            <TabWidget android:id="@android:id/tabs" android:orientation="horizontal" android:layout_width="fill_parent"  
              android:layout_height="wrap_content" />  
            <!-- FrameLayout的id屬性必須為 @android:id/tabcontent-->  
             <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent">  
                <TextView android:id="@+id/view1" android:layout_width="fill_parent" android:layout_height="fill_parent"/>  
                <TextView android:id="@+id/view2" android:layout_width="fill_parent" android:layout_height="fill_parent"/>  
                <TextView android:id="@+id/view3" android:layout_width="fill_parent" android:layout_height="fill_parent"/>  
             </FrameLayout>  
         </LinearLayout>  
    </TabHost>  

java程式碼:

   public class TabHostTest extends Activity {  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        // 獲取TabHost物件  
        TabHost tabHost = (TabHost) findViewById(R.id.tabhost);  
        // 如果沒有繼承TabActivity時,通過該種方法載入啟動tabHost  
        tabHost.setup();  
        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("第一個標籤",  
                getResources().getDrawable(R.drawable.icon)).setContent(  
                R.id.view1));  
  
        tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("第三個標籤")  
                .setContent(R.id.view3));  
  
        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("第二個標籤")  
                .setContent(R.id.view2));  
    }  
}

感謝本文所參考的博文,如果大家對tabhost還有什麼不太明白的話,可以參考本文!

Android之TabHost佈局

補充

針對於上面的程式碼,大家可能看到是標籤一直在選單的上方,那麼想要讓標籤在下方的話,直接將TabWidget放在FrameLayout的下方即可!
例如一下程式碼!

</FrameLayout>
    <TabWidget
    android:id="@android:id/tabs"
    android:layout_width="match_parent"
    android:layout_height="98px" >
    </TabWidget>

如果您對於我的部落格喜歡的花,歡迎你來關注我哦!

相關文章