底部菜单

来源:互联网 发布:鼓机节拍器软件 编辑:程序博客网 时间:2024/05/17 09:11

                          

主Activity:

package com.wyj.tabmenu;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class TabMenu extends TabActivity
{
// 创建TabHost
public TabHost mth;
public static final String TAB_HOME = "首页";
public static final String TAB_NEWS = "信息";
public static final String TAB_ABOUT = "关于";
public static final String TAB_SEARCH = "搜索";
public static final String TAB_PAGE = "更多";
public RadioGroup radioGroup;
@Override
public void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);
  // 去除标题
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.main);
  // 初始化底部菜单
  init();
  // 底部菜单点击事件
  clickevent();
}
/**
  * 每一个底部按钮点击事件,切换相应的界面
  */
private void clickevent()
{
  this.radioGroup = (RadioGroup) findViewById(R.id.main_radio);
  radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
  {
   @Override
   public void onCheckedChanged(RadioGroup group, int checkedId)
   {
    // 根据点击的按钮跳转到相应的界面
    switch (checkedId)
    {
    case R.id.radio_button0:
     mth.setCurrentTabByTag(TAB_HOME);
     break;
    case R.id.radio_button1:
     mth.setCurrentTabByTag(TAB_NEWS);
     break;
    case R.id.radio_button2:
     mth.setCurrentTabByTag(TAB_ABOUT);
     break;
    case R.id.radio_button3:
     mth.setCurrentTabByTag(TAB_SEARCH);
     break;
    case R.id.radio_button4:
     mth.setCurrentTabByTag(TAB_PAGE);
     break;
    }
   }
  });
}
/**
  * 实例化TabHost,往TabHost添加5个界面
  */
private void init()
{
  // 实例化TabHost
  mth = this.getTabHost();
  TabSpec ts1 = mth.newTabSpec(TAB_HOME).setIndicator(TAB_HOME);
  ts1.setContent(new Intent(TabMenu.this, Home_Page.class));
  mth.addTab(ts1);// 往TabHost中第一个底部菜单添加界面
  TabSpec ts2 = mth.newTabSpec(TAB_NEWS).setIndicator(TAB_NEWS);
  ts2.setContent(new Intent(TabMenu.this, News_Page.class));
  mth.addTab(ts2);
  TabSpec ts3 = mth.newTabSpec(TAB_ABOUT).setIndicator(TAB_ABOUT);
  ts3.setContent(new Intent(TabMenu.this, About_Page.class));
  mth.addTab(ts3);
  TabSpec ts4 = mth.newTabSpec(TAB_SEARCH).setIndicator(TAB_SEARCH);
  ts4.setContent(new Intent(TabMenu.this, Search_Page.class));
  mth.addTab(ts4);
  TabSpec ts5 = mth.newTabSpec(TAB_PAGE).setIndicator(TAB_PAGE);
  ts5.setContent(new Intent(TabMenu.this, More_Page.class));
  mth.addTab(ts5);
}
}

各个子Activity,需填充到主Activity中的界面,下面一个,其他全部一模一样:

package com.wyj.tabmenu;
import android.app.Activity;
import android.os.Bundle;
public class Home_Page extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);
  setContentView(R.layout.home_page);
}
}

主界面main.xml:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://csdn.com.net"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#667744" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <FrameLayout
            android:id="@+id/msg_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:visibility="gone" >
            <TextView
                android:id="@+id/tv_wb"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/titlebar_lightgray_bg" >
            </TextView>
        </FrameLayout>
        <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:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.0"
            android:visibility="gone" />
        <RadioGroup
            android:id="@+id/main_radio"
            android:layout_width="fill_parent"
            android:layout_height="45dp"
            android:layout_gravity="bottom"
            android:background="@drawable/main_tab_btn_bg"
            android:gravity="center_vertical"
            android:orientation="horizontal" >
            <RadioButton
                android:id="@+id/radio_button0"
                style="@style/main_tab_bottom"
                android:layout_height="fill_parent"
                android:layout_marginTop="2.0dip"
                android:drawableTop="@drawable/icon_1_n"
                android:tag="radio_button0"
                android:text="@string/main_home" />
            <RadioButton
                android:id="@+id/radio_button1"
                style="@style/main_tab_bottom"
                android:layout_height="fill_parent"
                android:layout_marginTop="2.0dip"
                android:drawableTop="@drawable/icon_2_n"
                android:tag="radio_button1"
                android:text="@string/main_news" />
            <RadioButton
                android:id="@+id/radio_button2"
                style="@style/main_tab_bottom"
                android:layout_height="fill_parent"
                android:layout_marginTop="2.0dip"
                android:drawableTop="@drawable/icon_3_n"
                android:tag="radio_button2"
                android:text="@string/main_my_info" />
            <RadioButton
                android:id="@+id/radio_button3"
                style="@style/main_tab_bottom"
                android:layout_height="fill_parent"
                android:layout_marginTop="2.0dip"
                android:drawableTop="@drawable/icon_4_n"
                android:tag="radio_button3"
                android:text="@string/menu_search" />
            <RadioButton
                android:id="@+id/radio_button4"
                style="@style/main_tab_bottom"
                android:layout_height="fill_parent"
                android:layout_marginTop="2.0dip"
                android:drawableTop="@drawable/icon_5_n"
                android:tag="radio_button4"
                android:text="@string/more" />
        </RadioGroup>
    </LinearLayout>
</TabHost>

子Activity-----

<LinearLayout xmlns:android=http://www.csdn.com.net
    xmlns:tools=http://www.csdn.com.net
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:background="@drawable/topbar"
        android:gravity="center_horizontal" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:gravity="center_vertical"
            android:text="XX首页"
            android:textColor="#FFFFFF"
            android:textSize="20dp" />
    </LinearLayout>
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="首页" />
</LinearLayout>

  values/style.xml   代码:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="main_tab_bottom">
        <item name="android:textSize">10.0dip</item>
        <item name="android:textColor">#ffffffff</item>
        <item name="android:ellipsize">marquee</item>
        <item name="android:gravity">center_horizontal</item>
        <item name="android:background">@drawable/home_btn_bg</item>
        <item name="android:paddingTop">1.0dip</item>
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>
        <item name="android:button">@null</item>
        <item name="android:singleLine">true</item>
        <item name="android:drawablePadding">1.0dip</item>
        <item name="android:layout_weight">1.0</item>
    </style>
</resources>

home_btn_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://www.csdn.net">
    <item android:state_focused="true" android:state_enabled="true" android:state_pressed="false" android:drawable="@drawable/home_btn_bg_s" />
    <item android:state_enabled="true" android:state_pressed="true" android:drawable="@drawable/home_btn_bg_s" />
    <item android:state_enabled="true" android:state_checked="true" android:drawable="@drawable/button_2" />  
</selector>

问题,当TabMenu extends TabActivity时,就无法直接监听返回按键了,后来改了一丢丢,在TabMenu 最下面加上如下代码就OK了:

// 监听返回键
@Override
public boolean dispatchKeyEvent(KeyEvent event)
{
  if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_BACK)
  {
   new AlertDialog.Builder(this).setCancelable(false).setTitle("温馨提示").setMessage("您确定要退出吗?").setPositiveButton("确定", new DialogInterface.OnClickListener()
   {
    public void onClick(DialogInterface dialog, int which)
    {
     finish();
    }
   }).setNegativeButton("取消", new DialogInterface.OnClickListener()
   {
    public void onClick(DialogInterface dialog, int which)
    {
    }
   }).show();
 return true;//  返回true/false有待考证??
  }
  return super.dispatchKeyEvent(event);
}
 http://www.apkbus.com/android-73873-1-1.html