溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

android基礎(chǔ)之TabSpec和TabHost

發(fā)布時(shí)間:2020-05-26 04:34:36 來(lái)源:網(wǎng)絡(luò) 閱讀:868 作者:hagar 欄目:移動(dòng)開(kāi)發(fā)

代碼如下:

布局代碼:

android基礎(chǔ)之TabSpec和TabHost

package com.example.tabhost;

import android.app.TabActivity;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.Menu;

import android.widget.TabHost;

import android.widget.TabHost.OnTabChangeListener;

import android.widget.TabHost.TabSpec;

import android.widget.Toast;


public class MainActivity extends TabActivity implements OnTabChangeListener {

private TabSpec ts1,ts2,ts3;//實(shí)例化3個(gè)分頁(yè)

private TabHost tableHost;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

tableHost=this.getTabHost();//實(shí)例化一個(gè)TableHost

//利用LayoutInflater將布局與分頁(yè)菜單一起顯示

LayoutInflater.from(this).inflate(R.layout.activity_main, tableHost.getTabContentView());

ts1=tableHost.newTabSpec("tabOne");//實(shí)例化一個(gè)分頁(yè)

ts1.setIndicator("Tab1");//設(shè)置此分頁(yè)顯示的標(biāo)題

ts1.setContent(R.id.btn);//設(shè)置此分頁(yè)的資源id

ts2=tableHost.newTabSpec("tabTwo");

//設(shè)置此分頁(yè)顯示的標(biāo)題和圖標(biāo)

ts2.setIndicator("Tab2",getResources().getDrawable(R.drawable.hagar3));

ts2.setContent(R.id.et);

ts3=tableHost.newTabSpec("TabThree");

ts3.setIndicator("tab3");

ts3.setContent(R.id.mylayout);//設(shè)置此分頁(yè)的布局id

tableHost.addTab(ts1);//菜單中添加ts1分頁(yè)

tableHost.addTab(ts2);

tableHost.addTab(ts3);

tableHost.setOnTabChangedListener(this);

}

public void onTabChanged(String tabId)

{

if(tabId.equals("tabOne"))

{

Toast.makeText(this, "分頁(yè)1", Toast.LENGTH_LONG).show();

}

if(tabId.equals("tabTwo"))

{

Toast.makeText(this, "分頁(yè)2", Toast.LENGTH_LONG).show();

}

if(tabId.equals("tabThree"))

{

Toast.makeText(this, "分頁(yè)3", Toast.LENGTH_LONG).show();

}

}

}


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI