基于android平台底部菜单实现

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

<?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);
}
}
}


[*]效果图
[img]

[img]http://dl2.iteye.com/upload/attachment/0092/0015/19a7345c-a8f6-3e2b-b96d-e5abe0274cd0.jpg[/img]

[/img]
[/list]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值