您的位置:首页 > 移动开发 > Android开发

Android进阶篇-自定义Menu(设置Menu的背景及文字属性)

2012-05-10 16:17 260 查看
系统自带的Menu有各种限制条件,如何设置Menu的背景和文字的各项属性呢?在不自定义的情况下,也是可以设置Menu的背景的。

/** 设置Menu的背景图 */
protected void setMenuBackground() {
this.getLayoutInflater().setFactory(
new android.view.LayoutInflater.Factory() {
public View onCreateView(String name, Context context,AttributeSet attrs) {
// 指定自定义inflate的对象
if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
try {
LayoutInflater f = getLayoutInflater();
final View view = f.createView(name, null,attrs);
new Handler().post(new Runnable() {
public void run() {
// 设置背景图片
view.setBackgroundResource(R.color.menu);
}
});
return view;
} catch (InflateException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
return null;
}
}
);
}


但是设置Menu显示文字的各项属性就比较麻烦了,为了更好的解决这些问题,我们最好还是采用自定义Menu的方法。

自定义Menu就是一个自定义的PopWindow:

public class TabMenu extends PopupWindow{
private LinearLayout mLayout;
private ImageView mImageView;
private TextView mTextView;

/**
* @param context 上下文
* @param onClickListener 单击事件
* @param resID 图片资源
* @param text 显示的文字
* @param fontSize 显示的文字大小
* @param fontColor 文字的颜色
* @param colorBgTabMenu 背景颜色
* @param aniTabMenu 消失的动画
* @return
*/
public TabMenu(Context context,OnClickListener onClickListener,int resID,String text,int fontSize,
int fontColor,int colorBgTabMenu,int aniTabMenu){
super(context);

mLayout=new LinearLayout(context);
mLayout.setOrientation(LinearLayout.VERTICAL);
mLayout.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
mLayout.setPadding(10, 10, 10, 10);

mTextView = new TextView(context);
mTextView.setTextSize((context.getResources().getDimensionPixelSize(fontSize)));
mTextView.setTextColor((context.getResources().getColor(fontColor)));
mTextView.setText(text);
mTextView.setGravity(Gravity.CENTER);
mTextView.setPadding(5, 5, 5, 5);

mImageView=new ImageView(context);
mImageView.setBackgroundResource(resID);

mLayout.addView(mImageView,new LinearLayout.LayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)));
mLayout.addView(mTextView);
mLayout.setOnClickListener(onClickListener);

this.setContentView(mLayout);
this.setWidth(LayoutParams.FILL_PARENT);
this.setHeight(LayoutParams.WRAP_CONTENT);
this.setBackgroundDrawable(new ColorDrawable(context.getResources().getColor(colorBgTabMenu)));
this.setAnimationStyle(aniTabMenu);
this.setFocusable(true);
}
}


而相应的在主界面,我们也应该进行Menu拦截操作。

private TabMenu tabMenu;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

tabMenu = new TabMenu(this, null, R.drawable.ic_launcher, "设置",
R.dimen.middle_text_size,R.color.blue,R.color.white,R.style.PopupAnimation);
}

/** 创建MENU */
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("menu");// 必须创建一项
return super.onCreateOptionsMenu(menu);
}

/** 拦截MENU */
public boolean onMenuOpened(int featureId, Menu menu) {
if (tabMenu != null) {
if (tabMenu.isShowing())
tabMenu.dismiss();
else {
tabMenu.showAtLocation(findViewById(R.id.LinearLayout01), Gravity.BOTTOM, 0, 0);
}
}
return false;// 返回为true 则显示系统menu
}


popup_animation.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="PopupAnimation" parent="android:Animation">
<item name="android:windowEnterAnimation">@anim/popup_enter</item>
<item name="android:windowExitAnimation">@anim/popup_exit</item>
</style>
</resources>


popup_enter.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="0" android:toYDelta="100%p" android:duration="1000" />
<alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="1000" />
</set>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: