Android底部菜單簡(jiǎn)單應(yīng)用
在Android中實(shí)現(xiàn)菜單功能有多種方法。
Options Menu:用戶按下menu Button時(shí)顯示的菜單。
Context Menu:用戶長(zhǎng)時(shí)間按下屏幕,所顯示出來的菜單也稱為上下文菜單。
Submenu:子菜單。
但是有時(shí)候這些內(nèi)置的菜單并不能滿足我們功能,這就需要自己自定義一種菜單。接下來我說的這種就是通過TabHost與RadioGroup結(jié)合完成的菜單。這也是很常用的一種底部菜單做法。先上圖:

Xml代碼
<?xml version="1.0" encoding="UTF-8"?>
<TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="1.0" />
<TabWidget
android:id="@android:id/tabs"
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.0" />
<RadioGroup
android:gravity="center_vertical"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:id="@+id/main_radio"
android:background="@drawable/maintab_toolbar_bg"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radio_button0"
android:tag="radio_button0"
android:layout_marginTop="2.0dip"
android:text="@string/alarm"
android:drawableTop="@drawable/icon_1"
style="@style/main_tab_bottom" />
<RadioButton
android:id="@+id/radio_button1"
android:tag="radio_button1"
android:layout_marginTop="2.0dip"
android:text="@string/message"
android:drawableTop="@drawable/icon_2"
style="@style/main_tab_bottom" />
<RadioButton
android:id="@+id/radio_button2"
android:tag="radio_button2"
android:layout_marginTop="2.0dip"
android:text="@string/photo"
android:drawableTop="@drawable/icon_3"
style="@style/main_tab_bottom" />
<RadioButton
android:id="@+id/radio_button3"
android:tag="radio_button3"
android:layout_marginTop="2.0dip"
android:text="@string/music"
android:drawableTop="@drawable/icon_4"
style="@style/main_tab_bottom" />
<RadioButton
android:id="@+id/radio_button4"
android:tag="radio_button4"
android:layout_marginTop="2.0dip"
android:text="@string/setting"
android:drawableTop="@drawable/icon_5"
style="@style/main_tab_bottom" />
</RadioGroup>
</LinearLayout>
</TabHost>
需要注意的是,如果用TabHost這個(gè)控件,其中有幾個(gè)ID是必須這么寫的,android:id=”@android:id/tabhost ;android:id=”@android:id/tabcontent” ;android:id=”@android:id/tabs” ;之所以要這么寫是因?yàn)樵赥abHost這個(gè)類中。需要實(shí)例化上述這個(gè)ID的控件??丛创a:
在TabActivity中有么個(gè)方法:
@Override
public void onContentChanged() {
super.onContentChanged();
mTabHost = (TabHost) findViewById(com.android.internal.R.id.tabhost);
if (mTabHost == null) {
throw new RuntimeException(
"Your content must have a TabHost whose id attribute is " +
"'android.R.id.tabhost'");
}
mTabHost.setup(getLocalActivityManager());
}
private void ensureTabHost() {
if (mTabHost == null) {
this.setContentView(com.android.internal.R.layout.tab_content);
}
}
當(dāng)內(nèi)容發(fā)生改變時(shí)它會(huì)調(diào)用這個(gè)方法,來更新列表或者其他視圖,而這個(gè)方法中需要實(shí)例化TabHost,所以必須通過ID為tabhost實(shí)例化。
再看看TabHost這個(gè)類中
public void setup() {
mTabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs);
if (mTabWidget == null) {
throw new RuntimeException(
"Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'");
}
// KeyListener to attach to all tabs. Detects non-navigation keys
// and relays them to the tab content.
mTabKeyListener = new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_DPAD_LEFT:
case KeyEvent.KEYCODE_DPAD_RIGHT:
case KeyEvent.KEYCODE_DPAD_UP:
case KeyEvent.KEYCODE_DPAD_DOWN:
case KeyEvent.KEYCODE_ENTER:
return false;
}
mTabContent.requestFocus(View.FOCUS_FORWARD);
return mTabContent.dispatchKeyEvent(event);
}
};
mTabWidget.setTabSelectionListener(new TabWidget.OnTabSelectionChanged() {
public void onTabSelectionChanged(int tabIndex, boolean clicked) {
setCurrentTab(tabIndex);
if (clicked) {
mTabContent.requestFocus(View.FOCUS_FORWARD);
}
}
});
mTabContent = (FrameLayout) findViewById(com.android.internal.R.id.tabcontent);
if (mTabContent == null) {
throw new RuntimeException(
"Your TabHost must have a FrameLayout whose id attribute is "
+ "'android.R.id.tabcontent'");
}
}
這個(gè)方法,是在增加選項(xiàng)卡之前由系統(tǒng)調(diào)用。在這個(gè)方法中需要通過tabs 這個(gè)ID實(shí)例化一個(gè)TabWidget,通過tabcontent這個(gè)ID實(shí)例化一個(gè)FrameLayout,用來放置選項(xiàng)卡內(nèi)容。所以這兩個(gè)ID也是固定的。
在上述布局文件中隱藏了系統(tǒng)默認(rèn)的Widget,取而代之的是帶有圖片的Button。
看一下主要代碼:
package com.iteye.androidtoast;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
public class MainActivity extends TabActivity implements OnCheckedChangeListener{
/** Called when the activity is first created. */
private TabHost mHost;
private RadioGroup radioderGroup;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maintabs);
//實(shí)例化TabHost
mHost=this.getTabHost();
//添加選項(xiàng)卡
mHost.addTab(mHost.newTabSpec("ONE").setIndicator("ONE")
.setContent(new Intent(this,OneActivity.class)));
mHost.addTab(mHost.newTabSpec("TWO").setIndicator("TWO")
.setContent(new Intent(this,TwoActivity.class)));
mHost.addTab(mHost.newTabSpec("THREE").setIndicator("THREE")
.setContent(new Intent(this,ThreeActivity.class)));
mHost.addTab(mHost.newTabSpec("FOUR").setIndicator("FOUR")
.setContent(new Intent(this,FourActivity.class)));
mHost.addTab(mHost.newTabSpec("FIVE").setIndicator("FIVE")
.setContent(new Intent(this,FiveActivity.class)));
radioderGroup = (RadioGroup) findViewById(R.id.main_radio);
radioderGroup.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId){
case R.id.radio_button0:
mHost.setCurrentTabByTag("ONE");
break;
case R.id.radio_button1:
mHost.setCurrentTabByTag("TWO");
break;
case R.id.radio_button2:
mHost.setCurrentTabByTag("THREE");
break;
case R.id.radio_button3:
mHost.setCurrentTabByTag("FOUR");
break;
case R.id.radio_button4:
mHost.setCurrentTabByTag("FIVE");
break;
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android底部菜單欄實(shí)現(xiàn)原理與代碼
- Android左右滑出菜單實(shí)例分析
- android popwindow實(shí)現(xiàn)左側(cè)彈出菜單層及PopupWindow主要方法介紹
- 基于Android實(shí)現(xiàn)點(diǎn)擊某個(gè)按鈕讓菜單選項(xiàng)從按鈕周圍指定位置彈出
- Android ListView長(zhǎng)按彈出菜單二種實(shí)現(xiàn)方式示例
- Android界面設(shè)計(jì)(APP設(shè)計(jì)趨勢(shì) 左側(cè)隱藏菜單右邊顯示content)
- Android開發(fā)技巧之我的菜單我做主(自定義菜單)
- Android仿QQ空間底部菜單示例代碼
- Android底部菜單欄實(shí)現(xiàn)的實(shí)例代碼
相關(guān)文章
Android仿高德首頁(yè)三段式滑動(dòng)效果的示例代碼
很多app都會(huì)使用三段式滑動(dòng),比如說高德的首頁(yè)和某寶等物流信息都是使用的三段式滑動(dòng)方式。本文將介紹如何實(shí)現(xiàn)這一效果,感興趣的可以學(xué)習(xí)一下2022-01-01
Android 6.0 無(wú)法在SD卡創(chuàng)建目錄的方法
今天小編就為大家分享一篇Android 6.0 無(wú)法在SD卡創(chuàng)建目錄的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08
Android 二維碼 生成和識(shí)別二維碼 附源碼下載
這篇文章主要介紹了Android 生成和識(shí)別二維碼的方法,提供源碼下載,需要的朋友可以參考下。2016-06-06
android 下載時(shí)文件名是中文和空格會(huì)報(bào)錯(cuò)解決方案
項(xiàng)目中遇到了下載文件文件名是中文而且還有空格如果不對(duì)連接進(jìn)行處理下載就會(huì)報(bào)錯(cuò)要想解決這個(gè)問題只需對(duì)你的url進(jìn)行編碼然后替換空格用編碼表示,感興趣的朋友可以詳細(xì)了解下2013-01-01
Android應(yīng)用程序四大組件之使用AIDL如何實(shí)現(xiàn)跨進(jìn)程調(diào)用Service
Android應(yīng)用程序的四大組件中Activity、BroadcastReceiver、ContentProvider、Service都可以進(jìn)行跨進(jìn)程,Android系統(tǒng)采用了遠(yuǎn)程過程調(diào)用(RPC)方式來實(shí)現(xiàn)跨進(jìn)程調(diào)用服務(wù)(Service),對(duì)于Service的跨進(jìn)程調(diào)用需要通過AIDL來實(shí)現(xiàn),關(guān)于如何實(shí)現(xiàn)aidl service請(qǐng)看本文介紹2015-10-10
Android 關(guān)于“NetworkOnMainThreadException”問題的原因分析及解決辦法
這篇文章主要介紹了Android 關(guān)于“NetworkOnMainThreadException”的相關(guān)知識(shí),本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-02-02
Android 讀取文件內(nèi)容實(shí)現(xiàn)方法總結(jié)
這篇文章主要介紹了Android 讀取文件內(nèi)容實(shí)現(xiàn)方法的相關(guān)資料,這里提供了幾種方法,大家可以選擇使用,需要的朋友可以參考下2016-10-10

