新聞中心
怎么在Android應(yīng)用中實現(xiàn)一個選項卡功能?針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
南開網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),南開網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為南開近1000家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的南開做網(wǎng)站的公司定做!
選項卡(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文件中修改代碼
關(guān)于怎么在Android應(yīng)用中實現(xiàn)一個選項卡功能問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。
本文名稱:怎么在Android應(yīng)用中實現(xiàn)一個選項卡功能
分享鏈接:http://fisionsoft.com.cn/article/jcscpj.html