Android 選項卡實現

來源:互聯網
上載者:User

Tab選項卡是一個非常方便的組件 今天查詢資料知道了android 選項卡的一種簡單實現. 本篇文章來源於好岸園it技術學習網 (http://www.hopean.com)  原文連結:http://www.hopean.com/devlop/ 下面是簡單的步驟. 1.在布局檔案中使用FrameLayout列出Tab組件以及Tab中的內容組件 2.Activity要繼承TabActivity 3.調用TabActivity的getTabHost( )方法來獲得TabHost對象 4.通過TabHost建立Tab選項 建立工程後首先, 修改strings.xml檔案 [html]  <?xml version="1.0" encoding="utf-8"?>  <resources>        <string name="app_name">AndroidTabSelector</string>      <string name="hello_world">Hello world!</string>      <string name="menu_settings">Settings</string>      <string name="test1">測試介面1</string>      <string name="test2">測試介面2</string>      <string name="test3">測試介面3</string>    </resources>     之後修改main.xml檔案,詳細代碼如下: [html]  <?xml version="1.0" encoding="utf-8"?>    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:id="@+id/FrameLayout01"      android:layout_width="wrap_content"      android:layout_height="wrap_content" >            <TabHost          android:id="@+id/TabHost01"          android:layout_width="wrap_content"          android:layout_height="wrap_content" />             <TextView          android:id="@+id/TextView01"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="@string/test1" />             <TextView          android:id="@+id/TextView02"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="@string/test2" />             <TextView          android:id="@+id/TextView03"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="@string/test3" />      </FrameLayout>     之後修改MainActivity.java檔案,java代碼如下: [java]  package com.example.androidtabselector;    import android.os.Bundle;  import android.app.Activity;  import android.view.Menu;  import android.app.TabActivity;  import android.os.Bundle;  import android.view.LayoutInflater;  import android.widget.TabHost;    public class MainActivity extends TabActivity {        @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          //setContentView(R.layout.activity_main);          TabHost tah = getTabHost();          // from(this)從TabActivity擷取LayoutInflater          // R.layout.main 存放Tab布局           // 通過TabHost獲得存放Tab標籤頁內容的FrameLayout          // 是否將inflate 加到根布局元素上            LayoutInflater.from(this).inflate(R.layout.activity_main,           tah.getTabContentView(), true);           //設定Tab標籤的內容和顯示內容            tah.addTab(tah.newTabSpec("tab1").setIndicator("選項1").setContent(R.id.TextView01));           tah.addTab(tah.newTabSpec("tab2").setIndicator("選項2").setContent(R.id.TextView02));             tah.addTab(tah.newTabSpec("tab3").setIndicator("選項3").setContent(R.id.TextView03));      }        @Override      public boolean onCreateOptionsMenu(Menu menu) {          // Inflate the menu; this adds items to the action bar if it is present.          getMenuInflater().inflate(R.menu.activity_main, menu);          return true;      }    }     至此就可以實現簡單的android 選項卡效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.