去年7月份 在无线点餐项目收尾后,美化界面过程中,想着能把Toast自定义一下 如下:
自定义Toast样式
/**
* 自定义taost样式,带icon的toast
*
* @param pic
* 显示的ICon
* @param str
* 提示信息
* @param time
* 显示信息时间的长短
*/
public void toastShow(int pic, String str, int time) {
Toast toast = Toast.makeText(getApplicationContext(), null, time);
LinearLayout layout = (LinearLayout) toast.getView();
layout.setOrientation(LinearLayout.HORIZONTAL);
ImageView imgView = new ImageView(getApplicationContext());
imgView.setImageResource(pic);
layout.addView(imgView);
TextView tv = new TextView(getApplicationContext());
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.FILL_PARENT));
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setPadding(10, 0, 0, 0);
tv.setText(str);
layout.addView(tv);
toast.show();
}
自定义OptionMenu样式 http://blog.csdn.net/pathuang68/article/details/6424801
@Override
public boolean onMenuItemClick(MenuItem item) {
if (item.isChecked()) {
item.setChecked( false );
}
if (!item.isChecked()) {
item.setChecked( true );
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem meal = menu.add( getString(R.string.makedeal) );
meal.setIcon(R.drawable.ordersmenu);
MenuItem save = menu.add( getString(R.string.save) );
save.setIcon(R.drawable.savemenu);
MenuItem backMain = menu.add( getString(R.string.menu_return) );
backMain.setIcon(R.drawable.exitmenu);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
String title = item.getTitle().toString();
List list = ((OrderList) getApplication()).getList();
if (title.equals(getString(R.string.makedeal))) {
if( list == null || list.size() == 0 ){
BaseActivity.toastShow(this,R.drawable.tb02, getString(R.string.menu_order_null), Toast.LENGTH_SHORT);
}else{
//下单,更新订单状态为1
makeMenuAndMakeOrder(1);
}
} else if (title.equals(getString(R.string.save))) {
if(list == null || list.size() == 0){
BaseActivity.toastShow(this,R.drawable.tb02, getString(R.string.menu_order_null), Toast.LENGTH_SHORT);
}else{
// 保存,更新订单状态为临时保存
makeMenuAndMakeOrder(0);
}
}else if(title.equals(getString(R.string.menu_return))){
//返回主界面
startActivity(new Intent(MenuListActivity.this,MainActivity.class));
}
return super .onOptionsItemSelected(item);
}
}