Menu及其自定义皮肤的处理
程序员文章站
2024-02-04 20:14:40
...
SimpleMenuActivity.java代码:
package org.penguin.study.android.menu;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.InflateException;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
/**
* 官方资料:http://developer.android.com/guide/topics/ui/menus.html
* Menu资料:http://developer.android.com/guide/topics/resources/menu-resource.html
*/
public class SimpleMenuActivity extends Activity {
private TextView messageTV;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
messageTV = (TextView) findViewById(R.id.messageTV);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// // groupId:分组的Id,如果不用分组就用0
// // ItemId:menu当中每一项的Id,这个Id应该是唯一的
// // order:排列的顺序
//
// menu.add(0, 19991, 1, "打开").setIcon(R.drawable.icon);
// menu.add(0, 19992, 2, "关闭").setIcon(R.drawable.delicious);
// menu.add(0, 19993, 0, "帮助").setIcon(R.drawable.google);
// int menuItemCounts = menu.size();
// messageTV.setText("Menu项数:" + menuItemCounts);
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.simple_menu, menu);
setMenuBackGround();
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
messageTV.setText(String.format("分组Id:%d、ItemId:%d、顺序:%d、标题:%s", item.getGroupId(), item.getItemId(), item.getOrder(), item.getTitle()));
return super.onOptionsItemSelected(item);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
System.out.println("KeyCode === " + keyCode + "、" + KeyEvent.KEYCODE_BACK);
if (keyCode == KeyEvent.KEYCODE_BACK) {
System.out.println("模拟器中ESC键是Back键");
// Toast.makeText(this.getApplicationContext(), "你按下了Home键",
// Toast.LENGTH_LONG).show();
// AlertDialog.Builder(SimpleMenuActivity.this).setTitle("提示标题").setMessage("这是提示内容").show();
new AlertDialog.Builder(SimpleMenuActivity.this).setTitle("您确认要退出吗?").setMessage("这是提示内容").setPositiveButton("退出", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.out.println("1 === " + dialog.getClass() + "、" + which + "、" + Dialog.BUTTON_POSITIVE);
}
}).setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.out.println("2 === " + dialog.getClass() + "、" + which + "、" + Dialog.BUTTON_NEGATIVE);
}
}).create().show();
return true;
}
return super.onKeyDown(keyCode, event);
}
// 核心代码:重写Layout.Factory.onCreateView()方法自定义布局
protected void setMenuBackGround() {
SimpleMenuActivity.this.getLayoutInflater().setFactory(new LayoutInflater.Factory() {
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")
|| name.equalsIgnoreCase("com.android.internal.view.menu.ListMenuItemView")) {
LayoutInflater layoutInflater = getLayoutInflater();
try {
final View view = layoutInflater.createView(name, null, attrs);
new Handler().post(new Runnable() {
@Override
public void run() {
view.setBackgroundColor(Color.BLUE);
// view.setBackgroundResource(R.drawable.icon);
}
});
return view;
} catch (InflateException e) {
System.err.println(e.getMessage());
} catch (ClassNotFoundException e) {
System.err.println(e.getMessage());
}
}
return null;
}
});
}
}
布局文件main.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello"
android:id="@+id/messageTV" />
</LinearLayout>
menu/simple_menu.xml代码:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/openMenuItem" android:icon="@drawable/icon" android:title="@string/open" /> <item android:id="@+id/helpMenuItem" android:icon="@drawable/google" android:title="@string/help" /> <item android:id="@+id/closeMenuItem" android:icon="@drawable/delicious" android:title="@string/close" /> <item android:title="废话1" /> <item android:title="废话2" /> <item android:title="废话3" /> <item android:title="更多1" /> <item android:title="更多2" /> <item android:title="更多3" /> <item android:title="更多4" /> <item android:title="更多5" /> <item android:title="更多6" /> <item android:title="更多7" /> <item android:title="更多8" /> <item android:title="更多9" /> </menu>
至于用到的图片(ico.png、google.png、delicious.png)均为png格式,从网上随便早点就ok了。一般放到drawable-hdpi目录就ok了。
strings.xml代码:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">有些事情,做了才有可能;如果仅是欣赏,我们只有回忆,但却不曾拥有</string> <string name="app_name">友好的菜单入门</string> <string name="help">帮助</string> <string name="open">打开</string> <string name="close">关闭</string> </resources>
上一篇: php 结果集的分页实现代码_PHP
下一篇: 数据类型不一致导致的SQL不走索引