欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android底部菜單簡單應(yīng)用

 更新時間:2017年02月08日 08:39:17   作者:vampire2777  
這篇文章主要為大家詳細(xì)介紹了Android底部菜單簡單應(yīng)用,具有一定的參考價值,感興趣的小伙伴們可以參考一下

在Android中實現(xiàn)菜單功能有多種方法。
Options Menu:用戶按下menu Button時顯示的菜單。
Context Menu:用戶長時間按下屏幕,所顯示出來的菜單也稱為上下文菜單。
Submenu:子菜單。
但是有時候這些內(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這個控件,其中有幾個ID是必須這么寫的,android:id=”@android:id/tabhost ;android:id=”@android:id/tabcontent” ;android:id=”@android:id/tabs” ;之所以要這么寫是因為在TabHost這個類中。需要實例化上述這個ID的控件??丛创a:

在TabActivity中有么個方法:

@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ā)生改變時它會調(diào)用這個方法,來更新列表或者其他視圖,而這個方法中需要實例化TabHost,所以必須通過ID為tabhost實例化。

再看看TabHost這個類中

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'"); 
   } 
 } 

這個方法,是在增加選項卡之前由系統(tǒng)調(diào)用。在這個方法中需要通過tabs 這個ID實例化一個TabWidget,通過tabcontent這個ID實例化一個FrameLayout,用來放置選項卡內(nèi)容。所以這兩個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); 
    //實例化TabHost 
    mHost=this.getTabHost(); 

    //添加選項卡 
    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; 
    }     
  } 
} 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 一篇文章就能了解Rxjava

    一篇文章就能了解Rxjava

    最近偶然接觸到了rxjava。關(guān)于rxjava,褒貶不一,有人說好用,有人說難用,不過它到底是什么?它有什么用?接下來,本文通過部分代碼及分析,向大家介紹rxjava及其相關(guān)內(nèi)容。
    2017-10-10
  • Android實現(xiàn)讀寫USB串口數(shù)據(jù)

    Android實現(xiàn)讀寫USB串口數(shù)據(jù)

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)讀寫USB串口數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • 使用Android Studio Gradle實現(xiàn)友盟多渠道打包

    使用Android Studio Gradle實現(xiàn)友盟多渠道打包

    這篇文章主要介紹了使用Android Studio Gradle實現(xiàn)友盟多渠道打包,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • 關(guān)于Android SDCard存儲的問題

    關(guān)于Android SDCard存儲的問題

    本篇文章小編為大家介紹,關(guān)于Android SDCard存儲的問題。需要的朋友參考下
    2013-04-04
  • Android 連接Wifi和創(chuàng)建Wifi熱點的實例

    Android 連接Wifi和創(chuàng)建Wifi熱點的實例

    本篇文章介紹了Android 連接Wifi和創(chuàng)建Wifi熱點,小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧。
    2016-10-10
  • Android實現(xiàn)串口通信

    Android實現(xiàn)串口通信

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)串口通信,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • Android ViewPager實現(xiàn)無限循環(huán)輪播廣告位Banner效果

    Android ViewPager實現(xiàn)無限循環(huán)輪播廣告位Banner效果

    這篇文章主要為大家詳細(xì)介紹了Android ViewPager實現(xiàn)無限循環(huán)輪播廣告位Banner效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Android特效之水波紋的實現(xiàn)

    Android特效之水波紋的實現(xiàn)

    今天我們主要講一講如何通過自定義View(以下簡稱WaveView)實現(xiàn) "咻咻咻" 式的水波紋擴散效果,感興趣的小伙伴們可以參考學(xué)習(xí)。
    2016-08-08
  • Flutter?App開發(fā)實現(xiàn)循環(huán)語句的方式實例

    Flutter?App開發(fā)實現(xiàn)循環(huán)語句的方式實例

    這篇文章主要為大家介紹了Flutter?App開發(fā)實現(xiàn)循環(huán)語句的方式示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • Android 調(diào)用發(fā)送短信的方法

    Android 調(diào)用發(fā)送短信的方法

    這篇文章主要介紹了Android 調(diào)用發(fā)送短信的方法的相關(guān)資料,主要實現(xiàn)Android 調(diào)用短信的使用,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-09-09

最新評論