這篇文章將為大家詳細(xì)講解有關(guān)如何在Android應(yīng)用中添加一個(gè)菜單功能,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
開發(fā)環(huán)境:AndroidStudio2.1.2
部分代碼(Activity)
public class NormalActivity extends AppCompatActivity { private FragmentTabHost mTabHost; private String[] mTabTitle = new String[]{"首頁", "軟件", "游戲", "管理"}; //菜單圖片樣式 private int[] mImageResId = new int[]{R.drawable.sel_tab_home, R.drawable.sel_tab_app, R.drawable.sel_tab_game, R.drawable.sel_tab_mag}; //要加載的Fragment private Class[] mFragClass = new Class[]{TabFragment.class, TabFragment.class, TabFragment.class, TabFragment.class}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_menu_normal); initTabHost(); } private void initTabHost() { mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); mTabHost.setup(this, getSupportFragmentManager(), R.id.relate_tab_content); for (int i = 0; i < mFragClass.length; i++) { //Bundle傳值到Fragment中,不傳值,則第三個(gè)參數(shù)為null Bundle bundle = new Bundle(); bundle.putString(TabFragment.FRAG_KEY, mTabTitle[i]); //第一參數(shù):菜單的文字&樣式 第二個(gè)參數(shù):需要加載Fragment,第三個(gè)參數(shù):需要傳到Fragment中的值 mTabHost.addTab(mTabHost.newTabSpec(mTabTitle[i]).setIndicator(getIndicator(i)), mFragClass[i], bundle); } //去掉間隔線 mTabHost.getTabWidget().setDividerDrawable(android.R.color.transparent); } //菜單的布局樣式 private View getIndicator(int index) { View view = LayoutInflater.from(this).inflate(R.layout.tabhost_indicator, null); ImageView imageView = (ImageView) view.findViewById(R.id.tab_icon); TextView title = (TextView) view.findViewById(R.id.tab_title); imageView.setImageResource(mImageResId[index]); title.setText(mTabTitle[index]); return view; }
關(guān)于如何在Android應(yīng)用中添加一個(gè)菜單功能就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
當(dāng)前標(biāo)題:如何在Android應(yīng)用中添加一個(gè)菜單功能-創(chuàng)新互聯(lián)
分享URL:http://jinyejixie.com/article26/disscg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、全網(wǎng)營銷推廣、ChatGPT、用戶體驗(yàn)、關(guān)鍵詞優(yōu)化、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容