我从网上粘的代码,提示有错
MenuAct cannot resolved to a type
MenuAct cannot resolved to a type
解决方案 »
- 【求教】Android.mk编译apk,第三方的jar包编译不进去!
- Android使用MediaRecorder的问题
- 关于在AppWidget中开启一个service的问题。
- 【源码分享】Android 画图 Drawable
- android3.0新增API onDragListener ,由于2.3没有这个API,如何在2.3如何实现?也就是在2.3上如何实现拖动?
- JCIFS 求助
- Android应用中怎么导出、导入独立文件
- Android textview显示特殊字符
- 如何直接访问腾讯信鸽推送的服务器
- 小白求教关于toast不显示的问题。绝对不是因为.show()
- androidIntent的使用
- NDK是不是java与C交互用的啊?
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
/**
* @author Sodino E-mail:[email protected]
* @version Time:2011-1-26 下午04:42:04
*/
public class MenuActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = new MenuInflater(getApplicationContext());
inflater.inflate(R.menu.menu, menu);
setMenuBackground();
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
String info = "";
switch (item.getItemId()) {
case R.id.menu_add:
info = "Add";
break;
case R.id.menu_delete:
info = "Delete";
break;
case R.id.menu_home:
info = "Home";
break;
case R.id.menu_help:
info = "Help";
break;
default:
info = "NULL";
break;
}
Toast toast = Toast.makeText(this, info, Toast.LENGTH_SHORT);
toast.show();
return super.onOptionsItemSelected(item);
}
// 关键代码为重写Layout.Factory.onCreateView()方法自定义布局
protected void setMenuBackground() {
MenuAct.this.getLayoutInflater().setFactory(new android.view.LayoutInflater.Factory() {
/**
* name - Tag name to be inflated.<br/>
* context - The context the view is being created in.<br/>
* attrs - Inflation attributes as specified in XML file.<br/>
*/
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.drawable.button4);
}
});
return view;
} catch (InflateException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
return null;
}
});
}
}