Android學習之TabWidget .

來源:互聯網
上載者:User

 今天和大家分享的是Android中的選項卡。

要使用選項卡就先瞭解兩個類:

TabHost: 它可以由TabActivity類的getTabHost獲得。

TabSpec: 代表選項卡每個Tab的TabSpec,它可以由TabHost.newTabSpec(string tag)獲得。

 

最後通過TabHost的AddTab(TabSpec)方法就可以添加Tab到選項卡上。

 

看代碼:

 

import android.app.TabActivity;<br />import android.content.Intent;<br />import android.os.Bundle;<br />import android.widget.TabHost;<br />import android.widget.TabHost.TabSpec;</p><p>public class TabView extends TabActivity {</p><p>private TabHost tabHost;<br />private TabSpec tabSpec;<br />private Intent intent;</p><p> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> findView();<br /> }</p><p> private void findView() {<br /> tabHost = this.getTabHost();</p><p> //增加第一個tab<br /> tabSpec = tabHost.newTabSpec("tab1");<br /> //顯示的字元<br /> tabSpec.setIndicator("tab1");<br /> //添加tab裡面的內容,也可以是Activity如第二個tab<br /> tabSpec.setContent(R.id.tab1);<br /> //添加在tab<br /> tabHost.addTab(tabSpec);</p><p> intent = new Intent(TabView.this,Tab.class);</p><p> //增加第二個tab<br /> tabSpec = tabHost.newTabSpec("tab2");<br /> tabSpec.setIndicator("tab2");<br /> //添加Activity到tab中<br /> tabSpec.setContent(intent);<br /> tabHost.addTab(tabSpec);<br /> }<br />}

 

xml檔案裡的格式一定要注意了:

這是必須的內容

<?xml version="1.0" encoding="utf-8"?><br /><TabHost android:id="@android:id/tabhost"<br />xmlns:android="http://schemas.android.com/apk/res/android"<br />android:layout_width="match_parent"<br />android:layout_height="match_parent"></p><p><LinearLayout<br />android:orientation="vertical"<br />android:layout_width="match_parent"<br />android:layout_height="match_parent"></p><p><TabWidget android:id="@android:id/tabs"<br />android:layout_width="match_parent"<br />android:layout_height="wrap_content"><br /></TabWidget></p><p><FrameLayout android:id="@android:id/tabcontent"<br />android:layout_width="match_parent"<br />android:layout_height="match_parent"></p><p><!-- tab1裡的內容 --><br /><LinearLayout android:id="@+id/tab1"<br />android:orientation="vertical"<br />android:layout_width="match_parent"<br />android:layout_height="match_parent"><br /><EditText android:id="@+id/edittext1"<br />android:layout_width="match_parent"<br />android:layout_height="wrap_content"<br />android:hint="我是第一個Tab裡的"><br /></EditText><br /><Button android:id="@+id/button1"<br />android:layout_width="match_parent"<br />android:layout_height="wrap_content"<br />android:text="我是也第一個Tab裡的"><br /></Button><br /></LinearLayout><br /></FrameLayout></p><p></LinearLayout><br /></TabHost>

 

在tab2中我增加的內容是一個顯示文字框的Activity:

import android.app.Activity;<br />import android.os.Bundle;<br />import android.widget.TextView;</p><p>public class Tab extends Activity {</p><p>public void onCreate(Bundle savedInstanceState){<br />super.onCreate(savedInstanceState);<br />TextView textView = new TextView(this);<br />textView.setText("我在tab2裡");<br />this.setContentView(textView);<br />}<br />}

 

 

看下:

 

總結:

這隻是一個簡單的實現,大家還可以多增加自己想要的功能!自己寫了程式之後對它的理解一定會更深的!

相關文章

聯繫我們

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