欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android TabWidget切換卡的實(shí)現(xiàn)應(yīng)用

 更新時(shí)間:2013年04月18日 17:25:43   作者:  
本篇文章小編為大家介紹,Android TabWidget切換卡的實(shí)現(xiàn)應(yīng)用。需要的朋友參考下

TabWidget類似于Android 中查看電話薄的界面,通過多個(gè)標(biāo)簽切換顯示不同內(nèi)容。要實(shí)現(xiàn)這一效果,首先要了解TabHost,它是一個(gè)用來存放多個(gè)Tab標(biāo)簽的容器。每一個(gè)Tab都可以對(duì)應(yīng)自己的布局,比如,電話薄中的Tab布局就是一個(gè)List的線性布局了。 
要使用TabHost,首先需要通過getTabHost方法來獲取TabHost的對(duì)象,然后通過addTab方法來向TabHost中添加 Tab。當(dāng)然每個(gè)Tab在切換時(shí)都會(huì)產(chǎn)生一個(gè)事件,要捕捉這個(gè)事件需要設(shè)置TabActivity的事件監(jiān)聽 setOnTabChangedListener。

1、布局文件

<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:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <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/textview1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="Linux"
        android:textColor="#FF0000" />

      <TextView
        android:id="@+id/textview2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="MAC"
        android:textColor="#385E0F" />

      <TextView
        android:id="@+id/textview3"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="Windows"
        android:textColor="#1E90FF" />
    </FrameLayout>
  </LinearLayout>

</TabHost>

2、修改MainActivity,注意是繼承自TabActivity

public class MainActivity extends TabActivity {

 private TabHost tabHost;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 tabHost = getTabHost();

 addTab();// 添加標(biāo)簽
 // 設(shè)置TabHost背景顏色
 tabHost.setBackgroundColor(Color.argb(150, 20, 80, 150));
 // 設(shè)置TabHost背景圖片資源
 tabHost.setBackgroundResource(R.drawable.ic_launcher);
 // 設(shè)置當(dāng)前顯示哪一個(gè)標(biāo)簽 我的理解就是當(dāng)你第一次啟動(dòng)程序默認(rèn)顯示那個(gè)標(biāo)簽 這里是指定的選項(xiàng)卡的ID從0開始
 tabHost.setCurrentTab(0);
 // 標(biāo)簽切換事件處理,setOnTabChangedListener 注意是標(biāo)簽切換事件不是點(diǎn)擊事件,而是從一個(gè)標(biāo)簽切換到另外一個(gè)標(biāo)簽會(huì)觸發(fā)的事件
 tabHost.setOnTabChangedListener(new OnTabChangeListener() {
  @Override
  public void onTabChanged(String tabId) {
  AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
  Dialog dia;
  builder.setTitle("提示");
  builder.setMessage("當(dāng)前選中了" + tabId + "標(biāo)簽");
  builder.setPositiveButton("確定", new OnClickListener() {
   @Override
   public void onClick(DialogInterface dialog, int which) {
   dialog.cancel();
   }
  });
  dia = builder.create();
  dia.show();
  }
 });
 }

 // 為TabHost添加標(biāo)簽 新建一個(gè)newTabSped(new TabSpec) 設(shè)置其標(biāo)簽和圖標(biāo)(setIndicator)、設(shè)置內(nèi)容(setContent)
 // TabSpec是TabHost的內(nèi)部類 TabHost對(duì)象的 newTabSpec()方法返回一個(gè)TabSpec對(duì)象
 // 源碼里邊是這么寫的 public TabSpec newTabSpec(String tag)
 // { return new TabSpec(tag); }
 private void addTab() {
 tabHost.addTab(tabHost
  .newTabSpec("tab1")
  .setIndicator("TAB1",
   getResources().getDrawable(R.drawable.ic_launcher))// setIndicator()此方法用來設(shè)置標(biāo)簽和圖表
  .setContent(R.id.textview1));
 // 指定內(nèi)容為一個(gè)TextView --->public TabHost.TabSpec setContent(int viewId) 此方法需要一個(gè) viewId 作為參數(shù)
 tabHost.addTab(tabHost
  .newTabSpec("tab2")
  .setIndicator("TAB2",
   getResources().getDrawable(R.drawable.ic_launcher))
  .setContent(R.id.textview2));

 tabHost.addTab(tabHost
  .newTabSpec("tab3")
  .setIndicator("TAB3",
   getResources().getDrawable(R.drawable.ic_launcher))
  .setContent(R.id.textview3));
 }
}

 3、運(yùn)行程序:如下!

相關(guān)文章

最新評(píng)論