android開發學習筆記系列(2)-android應用界面編程


前言

本篇博客將會簡要介紹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的朋友都有這樣的感受,就是我們用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>

如果您對於我的博客喜歡的花,歡迎你來關注我哦!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM