ITeye博客

基于android平台底部菜单实现

从百草园到三味书屋 · 2013-12-09 18:37 · android · 移动 · 移动开发 · Markdown

android,我发现几乎80%的应用程序,尤其是工具软件、管理软件等。都是一排底部菜单,然后切换来切换去,搞几个页面。这篇文章,是我接触android平台开发的第一篇移动方面的博客。很久了,都没有写的博客,因为自己结束了一段可笑的感情经历,终于解脱了。 我从不说废话,直接上代码。

配置文件


<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <include layout="@layout/activity_title" />

        <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="wrap_content"
            android:layout_gravity="bottom"
            android:gravity="center_vertical"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/radio_btn0"
                style="@style/main_tab_bottom"
                android:checked="true"
                android:drawableTop="@drawable/drawable_home"
                android:selectAllOnFocus="false"
                android:tag="radio_btn0"
                android:text="@string/btn0" />

            <RadioButton
                android:id="@+id/radio_btn1"
                style="@style/main_tab_bottom"
                android:drawableTop="@drawable/drawable_news"
                android:tag="radio_btn1"
                android:text="@string/btn1" />

            <RadioButton
                android:id="@+id/radio_btn2"
                style="@style/main_tab_bottom"
                android:drawableTop="@drawable/drawable_notice"
                android:tag="radio_btn2"
                android:text="@string/btn2" />

            <RadioButton
                android:id="@+id/radio_btn3"
                style="@style/main_tab_bottom"
                android:drawableTop="@drawable/drawable_topic"
                android:tag="radio_btn3"
                android:text="@string/btn3" />

            <RadioButton
                android:id="@+id/radio_btn4"
                style="@style/main_tab_bottom"
                android:drawableTop="@drawable/drawable_exit"
                android:tag="radio_btn4"
                android:text="@string/btn4" />
        </RadioGroup>
    </LinearLayout>

</TabHost>

activity


package com.example.test;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.TextView;

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity implements OnCheckedChangeListener{

	private TabHost mHost = null;
	private RadioGroup radioGroup = null;
	private RadioButton currentRadioBtn = null;
	private RadioButton previousRadioBtn = null;
	private TextView currentTitle = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		previousRadioBtn = (RadioButton)findViewById(R.id.radio_btn0);
		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)));
		radioGroup = (RadioGroup) findViewById(R.id.main_radio);   
		radioGroup.setOnCheckedChangeListener(this);   
		this.getWindow().setBackgroundDrawable(this.getResources().getDrawable(R.drawable.backColor));
	}

	@Override  
	public void onCheckedChanged(RadioGroup group, final int checkedId) {   
		currentRadioBtn = (RadioButton)findViewById(checkedId);
		 currentTitle = (TextView)findViewById(R.id.title);
		switch(checkedId){   
		case R.id.radio_btn0:   
			mHost.setCurrentTabByTag("ONE");   
			currentTitle.setText(R.string.btn0);
			break;   
		case R.id.radio_btn1:   
			mHost.setCurrentTabByTag("TWO");
			currentTitle.setText(R.string.btn1);
			break;   
		case R.id.radio_btn2:   
			mHost.setCurrentTabByTag("THREE");   
			currentTitle.setText(R.string.btn2);
			break;   
		case R.id.radio_btn3:   
			mHost.setCurrentTabByTag("FOUR");   
			currentTitle.setText(R.string.btn3);
			break;   
		case R.id.radio_btn4:   
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage(R.string.confirmExit).setCancelable(false).setPositiveButton("确定", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					MainActivity.this.finish();
				}
			}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					if(previousRadioBtn!=null){
						previousRadioBtn.setChecked(true);
					}
					dialog.cancel();
				}
			});
			builder.create().show();
			break;   
		}
		if(checkedId!=R.id.radio_btn4){
			previousRadioBtn = (RadioButton)findViewById(checkedId);
		}
	}   
}


效果图

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。