国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android切換卡TabWidget用法示例

2019-12-12 05:37:20
字體:
來源:轉載
供稿:網友

本文實例講述了Android切換卡TabWidget用法。分享給大家供大家參考,具體如下:

Tab選項卡類似與電話本的界面,通過多個標簽切換不同的內容,要實現這個效果,首先要知道TabHost,它是一個用來存放多個Tab標簽的容器,每一個Tab都可以對應自己的布局,比如,電話本中的Tab布局就是一個線性布局

要使用TabHost,首先要通過getTabHost方法獲取TabHost的對象,然后通過addTab方法來向TabHost中添加Tab,當然每個Tab在切換時都會產生一個事件,要捕捉這個事件,需要設置TabActivity的事件監聽setOnTabChangedListener

下面是個小例子:

TabTest.java:

package org.hualang.tab;import android.app.Activity;import android.app.TabActivity;import android.graphics.Color;import android.os.Bundle;import android.widget.TabHost;import android.widget.Toast;import android.widget.TabHost.OnTabChangeListener;public class TabTest extends TabActivity {  /** Called when the activity is first created. */  TabHost tabhost;  @Override  public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    //取得TabHost對象    tabhost = getTabHost();    //為TabHost添加標簽    //新建一個newTabSpec(newTabSpec)    //設置其標簽和圖標(setIndicator)    //設置內容(setContent)    tabhost.addTab(tabhost.newTabSpec("tab1")        .setIndicator("TAB 1",getResources().getDrawable(R.drawable.img1))        .setContent(R.id.text1));    tabhost.addTab(tabhost.newTabSpec("tab2")        .setIndicator("TAB 2",getResources().getDrawable(R.drawable.img2))        .setContent(R.id.text2));    tabhost.addTab(tabhost.newTabSpec("tab3")        .setIndicator("TAB 3",getResources().getDrawable(R.drawable.img3))        .setContent(R.id.text3));    //設置TabHost的背景顏色    //tabhost.setBackgroundColor(Color.argb(150,22,70,150));    //設置TabHost的背景圖片資源    tabhost.setBackgroundResource(R.drawable.bg0);    //設置當前顯示哪個標簽    tabhost.setCurrentTab(0);    //標簽切換事件處理,setOnTabChangedListener    tabhost.setOnTabChangedListener(new OnTabChangeListener()    {      public void onTabChanged(String tabId)      {        Toast toast=Toast.makeText(getApplicationContext(), "現在是"+tabId+"標簽", Toast.LENGTH_SHORT);        toast.show();      }    });  }}

main.xml:

<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="http://schemas.android.com/apk/res/android"  android:id="@android:id/tabhost"  android:layout_width="fill_parent"  android:layout_height="fill_parent">  <LinearLayout    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent">    <TabWidget      android:id="@android:id/tabs"      android:layout_width="fill_parent"      android:layout_height="wrap_content" />    <FrameLayout      android:id="@android:id/tabcontent"      android:layout_width="fill_parent"      android:layout_height="fill_parent">      <TextView        android:id="@+id/text1"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:text="選項卡1" />      <TextView        android:id="@+id/text2"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:text="選項卡2" />      <TextView        android:id="@+id/text3"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:text="選項卡3" />    </FrameLayout>  </LinearLayout></TabHost>

更多關于Android相關內容感興趣的讀者可查看本站專題:《Android控件用法總結》、《Android視圖View技巧總結》、《Android操作SQLite數據庫技巧總結》、《Android操作json格式數據技巧總結》、《Android數據庫操作技巧總結》、《Android文件操作技巧匯總》、《Android編程開發之SD卡操作方法匯總》、《Android開發入門與進階教程》及《Android資源操作技巧匯總

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阳曲县| 新巴尔虎右旗| 县级市| 庆安县| 额济纳旗| 邢台县| 叶城县| 望谟县| 屯门区| 新昌县| 德保县| 武宣县| 富民县| 游戏| 塔城市| 麦盖提县| 洛隆县| 申扎县| 临沭县| 库车县| 柘荣县| 大姚县| 邯郸市| 陵川县| 福安市| 海兴县| 通化县| 财经| 屯留县| 雷山县| 高雄县| 永平县| 平潭县| 佛教| 稻城县| 井冈山市| 卫辉市| 资阳市| 宣恩县| 岱山县| 成都市|