【Android】選項卡使用

來源:互聯網
上載者:User

一,建立三個Activity類

MainActiviey.java

package tianshuai.home_page;</p><p>import android.app.Activity;<br />import android.app.ActivityGroup;<br />import android.content.Intent;<br />import android.os.Bundle;<br />import android.view.View;<br />import android.widget.TabHost;<br />import android.widget.TabHost.TabSpec;</p><p>public class MainActivity extends ActivityGroup implements View.OnClickListener<br />{<br /> public static TabHost tab_host;<br /> @Override<br /> public void onCreate(Bundle savedInstanceState)<br /> {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> createTab();<br /> }</p><p> public void createTab()<br /> {<br /> tab_host = (TabHost) findViewById(R.id.tab_host);<br /> tab_host.setup(this.getLocalActivityManager());</p><p> tab_host.setOnClickListener(this);</p><p> TabSpec ts1 = tab_host.newTabSpec("手機酷站");<br /> ts1.setIndicator("現正播放", getResources().getDrawable(R.drawable.find));<br /> ts1.setContent(new Intent(this, cellphone_web.class));</p><p> tab_host.addTab(ts1);</p><p> TabSpec ts2 = tab_host.newTabSpec("軟體遊戲");<br /> ts2.setIndicator("本地曲庫", getResources().getDrawable(R.drawable.history));<br /> ts2.setContent(new Intent(this, software.class));</p><p> tab_host.addTab(ts2);<br /> tab_host.setCurrentTab(1);<br /> }</p><p> public void onClick(View arg0) {<br /> throw new UnsupportedOperationException("Not supported yet.");<br /> }</p><p>}
二,cellphone_web.java 跟 software.java均為繼承 Activity 的空類就可以

三,main.xml

<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"></p><p> <TabHost<br /> android:id="@+id/tab_host"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"><br /> <LinearLayout<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> android:padding="5dp"><br /> <FrameLayout<br /> android:id="@android:id/tabcontent"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:padding="5dp"<br /> android:layout_weight="1" /><br /> <TabWidget<br /> android:id="@android:id/tabs"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:layout_weight="0" /><br /> </LinearLayout><br /> </TabHost><br /></LinearLayout><br />
注意:TabWidget和FrameLayout 有不同的ID命名空間android:id="@android:id/idnames",這個是必須的因此TabHost才能自動找到它,Activity需要繼承TabActivity。

如果想讓選項卡在上方的話:

<FrameLayout<br /> android:id="@android:id/tabcontent"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:padding="5dp"<br /> android:layout_weight="0" /><br /> <TabWidget<br /> android:id="@android:id/tabs"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:layout_weight="1" />內容想在上邊,TabWidget 必須與 FrameLayout 換換位置

相關文章

聯繫我們

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