西西軟件下載最安全的下載網(wǎng)站、值得信賴的軟件下載站!

首頁編程開發(fā)Android → Android 手機(jī)電話薄TabWidget多標(biāo)簽切換卡效果

Android 手機(jī)電話薄TabWidget多標(biāo)簽切換卡效果

相關(guān)軟件相關(guān)文章發(fā)表評論 來源:西西整理時(shí)間:2013/4/17 22:43:47字體大。A-A+

作者:西西點(diǎn)擊:0次評論:2次標(biāo)簽: 標(biāo)簽切換

  • 類型:文件處理大。209KB語言:中文 評分:7.0
  • 標(biāo)簽:
立即下載

TabWidget類似于Android 中查看電話薄的界面,通過多個(gè)標(biāo)簽切換顯示不同內(nèi)容。要實(shí)現(xiàn)這一效果,首先要了解TabHost,它是一個(gè)用來存放多個(gè)Tab標(biāo)簽的容器。每一個(gè)Tab都可以對應(yīng)自己的布局,比如,電話薄中的Tab布局就是一個(gè)List的線性布局了。 
要使用TabHost,首先需要通過getTabHost方法來獲取TabHost的對象,然后通過addTab方法來向TabHost中添加 Tab。當(dāng)然每個(gè)Tab在切換時(shí)都會產(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)你第一次啟動程序默認(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)簽會觸發(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對象的 newTabSpec()方法返回一個(gè)TabSpec對象
	// 源碼里邊是這么寫的 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)行程序:如下!

    hosts修復(fù)軟件
    (60)hosts修復(fù)軟件
    文件是計(jì)算機(jī)中一個(gè)舉足輕重的文件,該文件有一個(gè)比較大的特點(diǎn)就是沒有擴(kuò)展名。經(jīng)常在一些電腦個(gè)性技巧以及其他領(lǐng)域方面會用到,西西提供文件修復(fù)工具軟件下載大全。官方介紹是一個(gè)沒有擴(kuò)展名的系統(tǒng)文件,可以用記事本等工具打開,其作用就是將一些常用的網(wǎng)址域名與其對應(yīng)的地址建立一個(gè)關(guān)聯(lián)數(shù)據(jù)庫,當(dāng)用戶在瀏覽器中輸入一個(gè)需要登錄的網(wǎng)址時(shí),系統(tǒng)會首先自動從文件中尋找對應(yīng)的地址,一旦找到系統(tǒng)會立即打開對應(yīng)網(wǎng)頁,如果沒有找...更多>>

    相關(guān)評論

    閱讀本文后您有什么感想? 已有人給出評價(jià)!

    • 8 喜歡喜歡
    • 3 頂
    • 1 難過難過
    • 5 囧
    • 3 圍觀圍觀
    • 2 無聊無聊

    熱門評論

    最新評論

    發(fā)表評論 查看所有評論(2)

    昵稱:
    表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
    字?jǐn)?shù): 0/500 (您的評論需要經(jīng)過審核才能顯示)