關於安卓開發選項卡的實現

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   ar   color   os   sp   

選項卡(TabHost)方便的在視窗上設定多個標籤頁,每個標籤頁相當於獲得一個與外部容器相同大小的組件擺放地區

通過這種方式,可以在一個容器中放置多組件。

建立4個java檔案並對應layout

建立主java ,代碼

 1 package lianxi; 2  3 import com.example.jichu_lianxi.R; 4  5 import android.app.TabActivity; 6 import android.content.Intent; 7 import android.content.res.Resources; 8 import android.os.Bundle; 9 import android.widget.TabHost;10 11 public class TobHost_lianxi extends TabActivity{12     @Override13     protected void onCreate(Bundle savedInstanceState) {14         // TODO Auto-generated method stub15         super.onCreate(savedInstanceState);16         17         //擷取當前Activity的標籤,該方法的實現已經執行了setContentView(com.android.internal.R.layout.tab_content);18         Resources resources = getResources();19         TabHost tabHost = getTabHost();20         TabHost.TabSpec spec;  21         /*22          * 對方法的解釋:23          * 1.     newTabSpec("artist")建立一個標籤項,其中artist為它的標籤標識符24          * 2.     setIndicator("標籤1", resources.getDrawable(R.drawable.bulb_off))25          *           顯示文本以及標籤上的表徵圖(該表徵圖不是一個圖片,而是一個xml檔案)26          */27         //添加第一個標籤28         Intent intent = new Intent(TobHost_lianxi.this,KeyOnclick.class);29         spec = tabHost.newTabSpec("keyonclick").setIndicator("標籤1", resources.getDrawable(R.drawable.bulb_off)).setContent(intent);30         tabHost.addTab(spec);//將標籤添加到標籤項中31         //添加第二個標籤32         Intent intent2 = new Intent(TobHost_lianxi.this,List_lianxi.class);33         spec = tabHost.newTabSpec("list").setIndicator("標籤2",resources.getDrawable(R.drawable.bulb_off)).setContent(intent2);34         tabHost.addTab(spec);35         //添加第三個標籤36         Intent intent3 = new Intent(TobHost_lianxi.this,ToggleButton_lianxi.class);37         spec = tabHost.newTabSpec("togglebutton").setIndicator("標籤3",resources.getDrawable(R.drawable.bulb_off)).setContent(intent3);38         tabHost.addTab(spec);39         //添加第四個標籤40         Intent intent4 = new Intent(TobHost_lianxi.this,ToggleButton_lianxi.class);41         spec = tabHost.newTabSpec("toggle").setIndicator("標籤4",resources.getDrawable(R.drawable.bulb_off)).setContent(intent4);42         tabHost.addTab(spec);43         44         //設定第一次開啟的預設顯示的標籤,參數與 .newTabSpec的參數匹配45         //tabHost.setCurrentTabByTag("toggle");46         //設定第一次開啟的預設顯示的標籤,參數代表其添加到標籤中的順序,位置從0開始47         tabHost.setCurrentTab(1);48         49     }50 51 }
主代碼


其中 KeyOnclick.java       List_lianxi.java        ToggleButton_lianxi.java 代碼不貼了

 

 

不要忘了在AndroidManifest.xml檔案中 修改代碼

<application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity android:name="lianxi.Mainactivity">            <intent-filter                                           >                <action android:name="android.intent.action.MAIN"/>                            <category android:name="android.intent.category.LAUNCHER" />                               </intent-filter>        </activity>        <activity android:name="lianxi.NewActivity"></activity>        <activity android:name="lianxi.AlertDialog_lianxi"></activity>        <activity android:name="lianxi.Notification_lianxi"></activity>        <activity android:name="lianxi.KeyOnclick"></activity>        <activity android:name="lianxi.List_lianxi"></activity>                <activity android:name="lianxi.ToggleButton_lianxi"></activity>         <activity android:name="lianxi.TobHost_lianxi"></activity>                            </application>


(第一張為標籤2。因為tabHost.setCurrentTab(1); 設定第2個添加的標籤項為預設顯示,從0開始算)

 

關於安卓開發選項卡的實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.