android 菜单 怎么让同一个选项菜单在任何一个页面都能显示.
写个BaseActivity,重写监听方法,然后其他页面继承此BaseActivity.请问具体怎么来...
写个BaseActivity,重写监听方法,然后其他页面继承此BaseActivity.请问具体怎么来
展开
3个回答
展开全部
自定义吧!!!或者用tabhost
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-01-15
展开全部
每页都加一个
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
给你个例子吧,自己研究
import java.util.ArrayList;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public abstract class BaseActivity extends Activity implements
View.OnClickListener {
private final static Integer DEFAULT_INDEX = 0;// 默认索引
protected int activityCase;
private ImageView home;// 首页按钮
private ImageView search;// 搜索按钮
private ImageView shopCar;// 购物车按钮
private ImageView myEbuy;// 我的帐户
private ImageView more;// 更多按钮
protected ProgressDialog progressDialog;// 进度条对话框
public static ShopcarDataOperation DBopt;
public ArrayList<ShopCarVo> goods_lists;
protected TextView textShopCarNum;// 显示购物车中商品数
protected Context context;// 上下文
private ThreadPoolManager threadPoolManager;// 线程池管理器
public static boolean islogin = false; // 判断是否登录
public static String username; // 用来检测保存登录的用户
public BaseActivity() {
threadPoolManager = ThreadPoolManager.getInstance();// 实例化纯种池管理器
}
/**
* Android生命周期回调方法-创建
*/
@Override
public void onCreate(Bundle paramBundle) {
super.onCreate(paramBundle);
requestWindowFeature(Window.FEATURE_NO_TITLE);// 窗口无标题栏的设置
// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
// WindowManager.LayoutParams.FLAG_FULLSCREEN);// 窗口全屏设置
context = getApplicationContext();// 得到整个应用的上下文
initView();
goods_lists = new ArrayList<ShopCarVo>();
}
/**
* 初始化各View
*/
private void initView() {
loadViewLayout();
if (isLoadBottomTab()) {
loadBottomTab();
selectedBottomTab(DEFAULT_INDEX);
}
findViewById();
setListener();
processLogic();
}
@Override
public void onClick(View v) {
selectedBottomTab(Constant.defaultIndex);
switch (v.getId()) {
// 跳转至我的购物车
case R.id.imgShoppingCar:
Const_homenative = false;
Const_morenative = false;
Const_searchnative = false;
Const_ebuynative = false;
selectedBottomTab(Constant.SHOPCAR);
if (Const_shopcarnative == false) {
if (islogin == true) {
System.out.println("============username============"
+ username);
DBopt = new ShopcarDataOperation(BaseActivity.this); // 拿到购物车内商品的数量,不为0就显示在购物车商品数量的TextView中去
// ShopCarVo vo = new ShopCarVo(13,"wwwww", "naifen", 34.78,
// "yuyu", 1, username);
// DBopt.insertIntoTable(ShopcarDatabase.GOODS_TABLE, vo);
// System.out.println("=============购物车=================");
goods_lists = DBopt.getDataListToShow(
ShopcarDatabase.GOODS_TABLE, username);
if(goods_lists != null){
if (goods_lists.size() > 0) {
textShopCarNum.setVisibility(TextView.VISIBLE);
textShopCarNum.setText(goods_lists.size() + "");
System.out.println("购物车的数量为: " + goods_lists.size());
Intent intent = new Intent(BaseActivity.this,
ShoppingCarOfUserActivity.class);
BaseActivity.this.startActivity(intent);
} else {
Intent intent = new Intent(BaseActivity.this,
ShoppingCarOfNoUserActivity.class);
BaseActivity.this.startActivity(intent);
textShopCarNum.setVisibility(TextView.GONE);
}
}else{
Toast.makeText(this, "购物车中暂时没有商品。。。", Toast.LENGTH_SHORT).show();
}
} else {
Intent intent = new Intent(BaseActivity.this,
LoginActivity.class);
startActivity(intent);
}
Const_shopcarnative = true;
}
if (!(this instanceof HomeActivity)) {
finish();
}
break;
// 跳转至我的账户
case R.id.imgEbuy:
Const_homenative = false;
Const_morenative = false;
Const_searchnative = false;
Const_shopcarnative = false;
if (islogin) {
selectedBottomTab(Constant.MYEBUY);
Intent intent1 = new Intent(this, MyaccountActivity.class);
startActivity(intent1);
} else {
if (Const_ebuynative == false) {
selectedBottomTab(Constant.MYEBUY);
Intent intent2 = new Intent(this, LoginActivity.class);
startActivity(intent2);
Const_ebuynative = true;
}
}
if (!(this instanceof HomeActivity)) {
this.finish();
}
break;
// 跳转至首页
case R.id.imgHome:
Const_ebuynative = false;
Const_morenative = false;
Const_searchnative = false;
Const_shopcarnative = false;
selectedBottomTab(Constant.HOME);
if (Const_homenative == false) {
Intent intent1 = new Intent(BaseActivity.this,
HomeActivity.class);
BaseActivity.this.startActivity(intent1);
Const_homenative = true;
}
if (!(this instanceof HomeActivity)) {
this.finish();
}
break;
// 跳转至搜索页面
case R.id.imgSearch:
Const_ebuynative = false;
Const_morenative = false;
Const_homenative = false;
Const_shopcarnative = false;
selectedBottomTab(Constant.SEARCH);
if (Const_searchnative == false) {
Intent intent2 = new Intent(BaseActivity.this,
SearchActivity.class);
BaseActivity.this.startActivity(intent2);
Const_searchnative = true;
}
if (!(this instanceof HomeActivity)) {
this.finish();
}
break;
// 跳转至更多页面
case R.id.imgMore:
Const_ebuynative = false;
Const_searchnative = false;
Const_homenative = false;
Const_shopcarnative = false;
selectedBottomTab(Constant.MORE);
if (Const_morenative == false) {
Intent moreintent = new Intent(this, MoreActivity.class);
startActivity(moreintent);
Const_morenative = true;
}
if (!(this instanceof HomeActivity)) {
this.finish();
}
break;
}
}
/**
*
* 处理从服务器获取的数据
*
*/
@SuppressWarnings("unchecked")
class BaseHandler extends Handler {
private Context context;
private DataCallback callBack;
private RequestVo reqVo;
public BaseHandler(Context context, DataCallback callBack,
RequestVo reqVo) {
this.context = context;
this.callBack = callBack;
this.reqVo = reqVo;
}
public void handleMessage(Message msg) {
if (msg.what == Constant.SUCCESS) {
closeProgressDialog();
if (msg.obj == null) {
CommonUtil.showInfoDialog(context,
getString(R.string.net_error));
} else {
callBack.processData(msg.obj, true);
}
} else if (msg.what == Constant.NET_FAILED) {
closeProgressDialog();
CommonUtil.showInfoDialog(context,
getString(R.string.net_error));
}
}
}
/**
* 从服务器获取数据的任务线程
* */
class BaseTask implements Runnable {
private Context context;
private RequestVo reqVo;
private Handler handler;
public BaseTask(Context context, RequestVo reqVo, Handler handler) {
this.context = context;
this.reqVo = reqVo;
this.handler = handler;
}
@Override
public void run() {
Object obj = null;
Message msg = new Message();
if (NetUtil.hasNetwork(context)) {
obj = NetUtil.post(reqVo);
msg.what = Constant.SUCCESS;
msg.obj = obj;
handler.sendMessage(msg);
} else {
msg.what = Constant.NET_FAILED;
msg.obj = obj;
handler.sendMessage(msg);
}
}
}
/**
* 加载底部Tab的
*/
protected void loadBottomTab() {
// 初始化控件
ImageView localImageView1 = (ImageView) findViewById(R.id.imgHome);// 首页
this.home = localImageView1;
ImageView localImageView2 = (ImageView) findViewById(R.id.imgSearch);// 搜索
this.search = localImageView2;
ImageView localImageView3 = (ImageView) findViewById(R.id.imgShoppingCar);// 购物车
this.shopCar = localImageView3;
ImageView localImageView4 = (ImageView) findViewById(R.id.imgEbuy);// 我的帐户
this.myEbuy = localImageView4;
ImageView localImageView5 = (ImageView) findViewById(R.id.imgMore);// 更多
this.more = localImageView5;
this.textShopCarNum = (TextView) findViewById(R.id.textShopCarNum);
// DBopt = new ShopcarDataOperation(this);
// //拿到购物车内商品的数量,不为0就显示在购物车商品数量的TextView中去
// goods_lists = DBopt.getDataListToShow(ShopcarDatabase.GOODS_TABLE,
// username);//???????????????????????????
// if(goods_lists.size() > 0){
// textShopCarNum.setVisibility(TextView.VISIBLE);
// textShopCarNum.setText(goods_lists.size()+"");
// System.out.println("购物车的数量为: "+goods_lists.size());
// }else{
// textShopCarNum.setVisibility(TextView.GONE);
// }
// 为控件添加点击事件监听
this.home.setOnClickListener(this);
this.search.setOnClickListener(this);
this.shopCar.setOnClickListener(this);
this.myEbuy.setOnClickListener(this);
this.more.setOnClickListener(this);
}
/**
* 根据传递的点击id,切换tab图片
*
* @param paramViewId
*/
protected void selectedBottomTab(int paramViewId) {
this.home.setBackgroundResource(R.drawable.tab_home_normal);
this.search.setBackgroundResource(R.drawable.tab_search_normal);
this.shopCar.setBackgroundResource(R.drawable.tab_shopping_normal);
this.myEbuy.setBackgroundResource(R.drawable.tab_myebuy_normal);
this.more.setBackgroundResource(R.drawable.tab_more_normal);
switch (paramViewId) {
case Constant.HOME:
this.home.setBackgroundResource(R.drawable.tab_home_pressed);
Constant.defaultIndex = Constant.HOME;
break;
case Constant.SEARCH:
this.search.setBackgroundResource(R.drawable.tab_search_pressed);
Constant.defaultIndex = Constant.SEARCH;
break;
case Constant.SHOPCAR:
this.shopCar.setBackgroundResource(R.drawable.tab_shopping_pressed);
Constant.defaultIndex = Constant.SHOPCAR;
break;
case Constant.MYEBUY:
this.myEbuy.setBackgroundResource(R.drawable.tab_myebuy_pressed);
Constant.defaultIndex = Constant.MYEBUY;
break;
case Constant.MORE:
this.more.setBackgroundResource(R.drawable.tab_more_pressed);
Constant.defaultIndex = Constant.MORE;
break;
}
}
public static boolean isIslogin() {
return islogin;
}
public static void setIslogin(boolean islogin) {
BaseActivity.islogin = islogin;
}
/**
* 是否加载底部tab,默认加载
*
* @return
*/
protected Boolean isLoadBottomTab() {
return true;
}
/**
*
* 数据回调接口
*
*/
public abstract interface DataCallback<T> {
public abstract void processData(T paramObject, boolean paramBoolean);
}
/**
* 从服务器上获取数据,并回调处理
*
* @param reqVo
* @param callBack
*/
protected void getDataFromServer(RequestVo reqVo, DataCallback callBack) {
showProgressDialog();
BaseHandler handler = new BaseHandler(this, callBack, reqVo);
BaseTask taskThread = new BaseTask(this, reqVo, handler);
this.threadPoolManager.addTask(taskThread);
}
/**
* 显示进度对话框
*/
protected void showProgressDialog() {
if ((!isFinishing()) && (this.progressDialog == null)) {
this.progressDialog = new ProgressDialog(this);
}
this.progressDialog.setTitle(getString(R.string.loadTitle));
this.progressDialog.setMessage(getString(R.string.LoadContent));
this.progressDialog.show();
}
/**
* 关闭进度对话框
*/
protected void closeProgressDialog() {
if (this.progressDialog != null)
this.progressDialog.dismiss();
}
/**
* 寻找控件的方法
*/
protected abstract void findViewById();
/**
* 为Activity加载布局文件的方法
*/
protected abstract void loadViewLayout();
/**
* 向后台请求数据
*/
protected abstract void processLogic();
/**
* 为控件设置监听
*/
protected abstract void setListener();
}
import java.util.ArrayList;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public abstract class BaseActivity extends Activity implements
View.OnClickListener {
private final static Integer DEFAULT_INDEX = 0;// 默认索引
protected int activityCase;
private ImageView home;// 首页按钮
private ImageView search;// 搜索按钮
private ImageView shopCar;// 购物车按钮
private ImageView myEbuy;// 我的帐户
private ImageView more;// 更多按钮
protected ProgressDialog progressDialog;// 进度条对话框
public static ShopcarDataOperation DBopt;
public ArrayList<ShopCarVo> goods_lists;
protected TextView textShopCarNum;// 显示购物车中商品数
protected Context context;// 上下文
private ThreadPoolManager threadPoolManager;// 线程池管理器
public static boolean islogin = false; // 判断是否登录
public static String username; // 用来检测保存登录的用户
public BaseActivity() {
threadPoolManager = ThreadPoolManager.getInstance();// 实例化纯种池管理器
}
/**
* Android生命周期回调方法-创建
*/
@Override
public void onCreate(Bundle paramBundle) {
super.onCreate(paramBundle);
requestWindowFeature(Window.FEATURE_NO_TITLE);// 窗口无标题栏的设置
// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
// WindowManager.LayoutParams.FLAG_FULLSCREEN);// 窗口全屏设置
context = getApplicationContext();// 得到整个应用的上下文
initView();
goods_lists = new ArrayList<ShopCarVo>();
}
/**
* 初始化各View
*/
private void initView() {
loadViewLayout();
if (isLoadBottomTab()) {
loadBottomTab();
selectedBottomTab(DEFAULT_INDEX);
}
findViewById();
setListener();
processLogic();
}
@Override
public void onClick(View v) {
selectedBottomTab(Constant.defaultIndex);
switch (v.getId()) {
// 跳转至我的购物车
case R.id.imgShoppingCar:
Const_homenative = false;
Const_morenative = false;
Const_searchnative = false;
Const_ebuynative = false;
selectedBottomTab(Constant.SHOPCAR);
if (Const_shopcarnative == false) {
if (islogin == true) {
System.out.println("============username============"
+ username);
DBopt = new ShopcarDataOperation(BaseActivity.this); // 拿到购物车内商品的数量,不为0就显示在购物车商品数量的TextView中去
// ShopCarVo vo = new ShopCarVo(13,"wwwww", "naifen", 34.78,
// "yuyu", 1, username);
// DBopt.insertIntoTable(ShopcarDatabase.GOODS_TABLE, vo);
// System.out.println("=============购物车=================");
goods_lists = DBopt.getDataListToShow(
ShopcarDatabase.GOODS_TABLE, username);
if(goods_lists != null){
if (goods_lists.size() > 0) {
textShopCarNum.setVisibility(TextView.VISIBLE);
textShopCarNum.setText(goods_lists.size() + "");
System.out.println("购物车的数量为: " + goods_lists.size());
Intent intent = new Intent(BaseActivity.this,
ShoppingCarOfUserActivity.class);
BaseActivity.this.startActivity(intent);
} else {
Intent intent = new Intent(BaseActivity.this,
ShoppingCarOfNoUserActivity.class);
BaseActivity.this.startActivity(intent);
textShopCarNum.setVisibility(TextView.GONE);
}
}else{
Toast.makeText(this, "购物车中暂时没有商品。。。", Toast.LENGTH_SHORT).show();
}
} else {
Intent intent = new Intent(BaseActivity.this,
LoginActivity.class);
startActivity(intent);
}
Const_shopcarnative = true;
}
if (!(this instanceof HomeActivity)) {
finish();
}
break;
// 跳转至我的账户
case R.id.imgEbuy:
Const_homenative = false;
Const_morenative = false;
Const_searchnative = false;
Const_shopcarnative = false;
if (islogin) {
selectedBottomTab(Constant.MYEBUY);
Intent intent1 = new Intent(this, MyaccountActivity.class);
startActivity(intent1);
} else {
if (Const_ebuynative == false) {
selectedBottomTab(Constant.MYEBUY);
Intent intent2 = new Intent(this, LoginActivity.class);
startActivity(intent2);
Const_ebuynative = true;
}
}
if (!(this instanceof HomeActivity)) {
this.finish();
}
break;
// 跳转至首页
case R.id.imgHome:
Const_ebuynative = false;
Const_morenative = false;
Const_searchnative = false;
Const_shopcarnative = false;
selectedBottomTab(Constant.HOME);
if (Const_homenative == false) {
Intent intent1 = new Intent(BaseActivity.this,
HomeActivity.class);
BaseActivity.this.startActivity(intent1);
Const_homenative = true;
}
if (!(this instanceof HomeActivity)) {
this.finish();
}
break;
// 跳转至搜索页面
case R.id.imgSearch:
Const_ebuynative = false;
Const_morenative = false;
Const_homenative = false;
Const_shopcarnative = false;
selectedBottomTab(Constant.SEARCH);
if (Const_searchnative == false) {
Intent intent2 = new Intent(BaseActivity.this,
SearchActivity.class);
BaseActivity.this.startActivity(intent2);
Const_searchnative = true;
}
if (!(this instanceof HomeActivity)) {
this.finish();
}
break;
// 跳转至更多页面
case R.id.imgMore:
Const_ebuynative = false;
Const_searchnative = false;
Const_homenative = false;
Const_shopcarnative = false;
selectedBottomTab(Constant.MORE);
if (Const_morenative == false) {
Intent moreintent = new Intent(this, MoreActivity.class);
startActivity(moreintent);
Const_morenative = true;
}
if (!(this instanceof HomeActivity)) {
this.finish();
}
break;
}
}
/**
*
* 处理从服务器获取的数据
*
*/
@SuppressWarnings("unchecked")
class BaseHandler extends Handler {
private Context context;
private DataCallback callBack;
private RequestVo reqVo;
public BaseHandler(Context context, DataCallback callBack,
RequestVo reqVo) {
this.context = context;
this.callBack = callBack;
this.reqVo = reqVo;
}
public void handleMessage(Message msg) {
if (msg.what == Constant.SUCCESS) {
closeProgressDialog();
if (msg.obj == null) {
CommonUtil.showInfoDialog(context,
getString(R.string.net_error));
} else {
callBack.processData(msg.obj, true);
}
} else if (msg.what == Constant.NET_FAILED) {
closeProgressDialog();
CommonUtil.showInfoDialog(context,
getString(R.string.net_error));
}
}
}
/**
* 从服务器获取数据的任务线程
* */
class BaseTask implements Runnable {
private Context context;
private RequestVo reqVo;
private Handler handler;
public BaseTask(Context context, RequestVo reqVo, Handler handler) {
this.context = context;
this.reqVo = reqVo;
this.handler = handler;
}
@Override
public void run() {
Object obj = null;
Message msg = new Message();
if (NetUtil.hasNetwork(context)) {
obj = NetUtil.post(reqVo);
msg.what = Constant.SUCCESS;
msg.obj = obj;
handler.sendMessage(msg);
} else {
msg.what = Constant.NET_FAILED;
msg.obj = obj;
handler.sendMessage(msg);
}
}
}
/**
* 加载底部Tab的
*/
protected void loadBottomTab() {
// 初始化控件
ImageView localImageView1 = (ImageView) findViewById(R.id.imgHome);// 首页
this.home = localImageView1;
ImageView localImageView2 = (ImageView) findViewById(R.id.imgSearch);// 搜索
this.search = localImageView2;
ImageView localImageView3 = (ImageView) findViewById(R.id.imgShoppingCar);// 购物车
this.shopCar = localImageView3;
ImageView localImageView4 = (ImageView) findViewById(R.id.imgEbuy);// 我的帐户
this.myEbuy = localImageView4;
ImageView localImageView5 = (ImageView) findViewById(R.id.imgMore);// 更多
this.more = localImageView5;
this.textShopCarNum = (TextView) findViewById(R.id.textShopCarNum);
// DBopt = new ShopcarDataOperation(this);
// //拿到购物车内商品的数量,不为0就显示在购物车商品数量的TextView中去
// goods_lists = DBopt.getDataListToShow(ShopcarDatabase.GOODS_TABLE,
// username);//???????????????????????????
// if(goods_lists.size() > 0){
// textShopCarNum.setVisibility(TextView.VISIBLE);
// textShopCarNum.setText(goods_lists.size()+"");
// System.out.println("购物车的数量为: "+goods_lists.size());
// }else{
// textShopCarNum.setVisibility(TextView.GONE);
// }
// 为控件添加点击事件监听
this.home.setOnClickListener(this);
this.search.setOnClickListener(this);
this.shopCar.setOnClickListener(this);
this.myEbuy.setOnClickListener(this);
this.more.setOnClickListener(this);
}
/**
* 根据传递的点击id,切换tab图片
*
* @param paramViewId
*/
protected void selectedBottomTab(int paramViewId) {
this.home.setBackgroundResource(R.drawable.tab_home_normal);
this.search.setBackgroundResource(R.drawable.tab_search_normal);
this.shopCar.setBackgroundResource(R.drawable.tab_shopping_normal);
this.myEbuy.setBackgroundResource(R.drawable.tab_myebuy_normal);
this.more.setBackgroundResource(R.drawable.tab_more_normal);
switch (paramViewId) {
case Constant.HOME:
this.home.setBackgroundResource(R.drawable.tab_home_pressed);
Constant.defaultIndex = Constant.HOME;
break;
case Constant.SEARCH:
this.search.setBackgroundResource(R.drawable.tab_search_pressed);
Constant.defaultIndex = Constant.SEARCH;
break;
case Constant.SHOPCAR:
this.shopCar.setBackgroundResource(R.drawable.tab_shopping_pressed);
Constant.defaultIndex = Constant.SHOPCAR;
break;
case Constant.MYEBUY:
this.myEbuy.setBackgroundResource(R.drawable.tab_myebuy_pressed);
Constant.defaultIndex = Constant.MYEBUY;
break;
case Constant.MORE:
this.more.setBackgroundResource(R.drawable.tab_more_pressed);
Constant.defaultIndex = Constant.MORE;
break;
}
}
public static boolean isIslogin() {
return islogin;
}
public static void setIslogin(boolean islogin) {
BaseActivity.islogin = islogin;
}
/**
* 是否加载底部tab,默认加载
*
* @return
*/
protected Boolean isLoadBottomTab() {
return true;
}
/**
*
* 数据回调接口
*
*/
public abstract interface DataCallback<T> {
public abstract void processData(T paramObject, boolean paramBoolean);
}
/**
* 从服务器上获取数据,并回调处理
*
* @param reqVo
* @param callBack
*/
protected void getDataFromServer(RequestVo reqVo, DataCallback callBack) {
showProgressDialog();
BaseHandler handler = new BaseHandler(this, callBack, reqVo);
BaseTask taskThread = new BaseTask(this, reqVo, handler);
this.threadPoolManager.addTask(taskThread);
}
/**
* 显示进度对话框
*/
protected void showProgressDialog() {
if ((!isFinishing()) && (this.progressDialog == null)) {
this.progressDialog = new ProgressDialog(this);
}
this.progressDialog.setTitle(getString(R.string.loadTitle));
this.progressDialog.setMessage(getString(R.string.LoadContent));
this.progressDialog.show();
}
/**
* 关闭进度对话框
*/
protected void closeProgressDialog() {
if (this.progressDialog != null)
this.progressDialog.dismiss();
}
/**
* 寻找控件的方法
*/
protected abstract void findViewById();
/**
* 为Activity加载布局文件的方法
*/
protected abstract void loadViewLayout();
/**
* 向后台请求数据
*/
protected abstract void processLogic();
/**
* 为控件设置监听
*/
protected abstract void setListener();
}
更多追问追答
追答
里面主要用到的是textview和button,这些框是textview的背景图片。布局的话主要是相对布局嵌套线性布局,然后线性布局中在嵌套多个相对布局。
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询