Android開發(fā)之選項卡功能的實現(xiàn)方法示例
本文實例講述了Android選項卡功能的實現(xiàn)方法。分享給大家供大家參考,具體如下:
選項卡(TabHost)方便的在窗口上設(shè)置多個標簽頁,每個標簽頁相當于獲得一個與外部容器相同大小的組件擺放區(qū)域
通過這種方式,可以在一個容器中放置多組件。
創(chuàng)建4個java文件并對應(yīng)layout
創(chuàng)建主java ,代碼
package lianxi;
import com.example.jichu_lianxi.R;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
public class TobHost_lianxi extends TabActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//獲取當前Activity的標簽,該方法的實現(xiàn)已經(jīng)執(zhí)行了setContentView(com.android.internal.R.layout.tab_content);
Resources resources = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
/*
* 對方法的解釋:
* 1. newTabSpec("artist")創(chuàng)建一個標簽項,其中artist為它的標簽標識符
* 2. setIndicator("標簽1", resources.getDrawable(R.drawable.bulb_off))
* 顯示文本以及標簽上的圖標(該圖標不是一個圖片,而是一個xml文件)
*/
//添加第一個標簽
Intent intent = new Intent(TobHost_lianxi.this,KeyOnclick.class);
spec = tabHost.newTabSpec("keyonclick").setIndicator("標簽1", resources.getDrawable(R.drawable.bulb_off)).setContent(intent);
tabHost.addTab(spec);//將標簽添加到標簽項中
//添加第二個標簽
Intent intent2 = new Intent(TobHost_lianxi.this,List_lianxi.class);
spec = tabHost.newTabSpec("list").setIndicator("標簽2",resources.getDrawable(R.drawable.bulb_off)).setContent(intent2);
tabHost.addTab(spec);
//添加第三個標簽
Intent intent3 = new Intent(TobHost_lianxi.this,ToggleButton_lianxi.class);
spec = tabHost.newTabSpec("togglebutton").setIndicator("標簽3",resources.getDrawable(R.drawable.bulb_off)).setContent(intent3);
tabHost.addTab(spec);
//添加第四個標簽
Intent intent4 = new Intent(TobHost_lianxi.this,ToggleButton_lianxi.class);
spec = tabHost.newTabSpec("toggle").setIndicator("標簽4",resources.getDrawable(R.drawable.bulb_off)).setContent(intent4);
tabHost.addTab(spec);
//設(shè)置第一次打開的默認顯示的標簽,參數(shù)與 .newTabSpec的參數(shù)匹配
//tabHost.setCurrentTabByTag("toggle");
//設(shè)置第一次打開的默認顯示的標簽,參數(shù)代表其添加到標簽中的順序,位置從0開始
tabHost.setCurrentTab(1);
}
}
其中 KeyOnclick.java、List_lianxi.java、ToggleButton_lianxi.java 代碼不貼了
不要忘了在AndroidManifest.xml文件中修改代碼
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name="lianxi.Mainactivity">
<intent-filter
>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="lianxi.NewActivity"></activity>
<activity android:name="lianxi.AlertDialog_lianxi"></activity>
<activity android:name="lianxi.Notification_lianxi"></activity>
<activity android:name="lianxi.KeyOnclick"></activity>
<activity android:name="lianxi.List_lianxi"></activity>
<activity android:name="lianxi.ToggleButton_lianxi"></activity>
<activity android:name="lianxi.TobHost_lianxi"></activity>
</application>
效果圖(第一張為標簽2。因為tabHost.setCurrentTab(1); 設(shè)置第2個添加的標簽項為默認顯示,從0開始算)



更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
相關(guān)文章
Flutter學(xué)習(xí)之創(chuàng)建一個內(nèi)嵌的navigation詳解
我們在flutter中可以使用Navigator.push或者Navigator.pushNamed方法來向Navigator中添加不同的頁面,從而達到頁面調(diào)整的目的。本文就來聊聊如何創(chuàng)建一個內(nèi)嵌的navigation吧2023-03-03
Android仿Iphone屏幕底部彈出半透明PopupWindow效果
這篇文章主要為大家詳細介紹了Android仿Iphone屏幕底部彈出半透明PopupWindow效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
淺談Android AsyncTask內(nèi)存安全的一種使用方式
這篇文章主要介紹了淺談Android AsyncTask內(nèi)存安全的一種使用方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08
android實現(xiàn)雙日期選擇控件(可隱藏日,只顯示年月)
本篇文章主要介紹了android實現(xiàn)雙日期選擇控件(可隱藏日,只顯示年月) ,非常具有實用價值,需要的朋友可以參考下。2017-01-01

