初日に日記を学習Androidの開発者

まず、タブ

タブを作成するには、アンドリュースの4つのステップ:

図1に示すように、レイアウトファイルにTabHost、TabWidgetとTabContentコンポーネントを追加

      使用TabHostが一体アップ含み、その後TabWidgetを加え線形垂直レイアウトマネージャを使用して、フレームアセンブリレイアウトマネージャでframeLayout

<TabHostのxmlns:アンドロイド= "http://schemas.android.com/apk/res/android" 
    のxmlns:アプリ= "http://schemas.android.com/apk/res-auto" 
    のxmlns:ツール= "HTTP ://schemas.android.com/tools " 
    アンドロイド:ID = "@アンドロイド:ID / tabhost" 
    アンドロイド:layout_width = "match_parent" 
    のandroid:layout_height = "match_parent" 
    ツール:コンテキスト="。MainActivity "> 
    <のLinearLayout 
        アンドロイド: layout_width = "match_parent" 
        アンドロイド:layout_height = "match_parent" 
        アンドロイド:オリエンテーション= "垂直"> 
        <TabWidget 
            アンドロイド:ID = "@アンドロイド:ID /タブ" 
            アンドロイド:layout_width =」match_parent」 match_parent」 
            アンドロイド:layout_height = "wrap_content">
        </ TabWidget> 
        <でframeLayout
            アンドロイド:ID = "@アンドロイド:ID / tabcontent" 
            アンドロイド:layout_width = "wrap_content" 
            のandroid:layout_height = "wrap_content"> 
        </でframeLayout> 
    </のLinearLayout> 
</ TabHost>

各タブのためのXMLレイアウトファイルに記述された2、

  tab1.xml:線形レイアウトマネージャImageViewの延伸なしのSrc画像を充填する、コンポーネントを追加します。

<のLinearLayoutのxmlns:アンドロイド= "http://schemas.android.com/apk/res/android" 
    アンドロイド:オリエンテーション= "垂直" 
    アンドロイド:ID = "@ + ID /左" 
    アンドロイド:layout_width = "match_parent" 
    アンドロイド。 layout_height = "match_parent"> 
    <ImageViewのの
        アンドロイド:layout_width = "match_parent" 
        のandroid:layout_height = "match_parent" 
        アンドロイド:SRC = "@の描画可能/ biaoqian_left"> 
    </ ImageViewの> 
</のLinearLayout>

  tab2.xml:同tab1.xml

<のLinearLayoutのxmlns:アンドロイド= "http://schemas.android.com/apk/res/android" 
    アンドロイド:オリエンテーション= "垂直" 
    アンドロイド:ID = "@ + ID /右" 
    アンドロイド:layout_width = "match_parent" 
    アンドロイド。 layout_height = "match_parent"> 
<ImageViewのの
    アンドロイド:layout_width = "match_parent" 
    のandroid:layout_height = "match_parent" 
    アンドロイド:SRC = "@の描画可能/ biaoqian_right"> 
</ ImageViewの> 
</のLinearLayout>

図3に示すように、コンポーネントを取得し、初期化TabHost

TabHost = TabHost findViewById(android.R.id.tabhost); 
 tabHost.setup(); //初期化

4、TabHostオブジェクトに個々のタブを追加

//使用LayoutInflaterオブジェクトは、タブを追加する方法を膨張
        LayoutInflater LayoutInflater = LayoutInflater.from(この); 
        layoutInflater.inflate(R.layout.tab1、tabHost.getTabContentView()); 
        layoutInflater.inflate(R.layout.tab2、TabHost .getTabContentView()); 
        //対応するタブをロードするオブジェクトTabHost addTabメソッドを使用
        tabHost.addTab(tabHost.newTabSpec( "TAB1") setIndicatorを( " 絵を特色" ).setContent(R.id.left)) ; 
        tabHost.addTab(tabHost.newTabSpec( "TAB2")setIndicator( "提出ピクチャ").setContent(R.id.right)。) 。

最終結果:

       

おすすめ

転載: www.cnblogs.com/liblogs/p/11447662.html