自己动手新增一个工程
<shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><img alt="" src="https://p-blog.youkuaiyun.com/images/p_blog_youkuaiyun.com/huareal/menu1.JPG"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape id="_x0000_i1025" style="WIDTH: 204.75pt; HEIGHT: 177.75pt" type="#_x0000_t75"><imagedata o:title="" src="file:///C:/DOCUME~1/hu/LOCALS~1/Temp/msohtml1/01/clip_image001.png"></imagedata></shape>
技巧:默认创建一个类,R.java不要修改,将Nodepadv2.java中对R的import 路径删除,重新通过Ctrl+shift+O导入,选择org.huareal.demo.R[具体随不同项目而定]
如果想改变R.java中的内容,修正res目录下的内容的
其中strings.xml内容修改为:
<?xmlversion="1.0"encoding="utf-8"?>
<resources>
<stringname="app_name">GGame</string>
<stringname="menu_begin">begin Game</string>
<stringname="menu_pause">pause Game</string>
<stringname="menu_exit">exit Game</string>
</resources>
其中的menu_beginmenu_pause,menu_exit都属于后续添加的。为了使R.java具有上述内容,通过选择project中clean操作,清理对应的工程,R.java将自动更新。
该实例中,首先根据练习1中,增加相应的三个菜单,分别对应上述的三个menu
为了显示菜单,通过实现方法:
@Override
publicboolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
//return super.onCreateOptionsMenu(menu);
boolean result = super.onCreateOptionsMenu(menu);
Log.i("info", "NotePad MenuCreate ");
menu.add(0, BEGIN_ID, R.string.menu_begin);
menu.add(0,PAUSE_ID,R.string.menu_pause);
menu.add(0,EXIT_ID,R.string.menu_exit);
return result;
}
点击每个菜单的操作如下:
@Override
publicboolean onOptionsItemSelected(Item item) {
Log.i("info", "NotePad Menu select ");
switch (item.getId()) {
caseBEGIN_ID:
beginGame();
break;
casePAUSE_ID:
pauseGame();
break;
caseEXIT_ID:
exitGame();
break;
default:
Log.i("i","无效的菜单选项");
break;
}
returnsuper.onOptionsItemSelected(item);
}
publicvoid beginGame(){
Log.i("info", "Begin game ");
}
publicvoid pauseGame(){
Log.i("info", "Pause game");
}
publicvoid exitGame(){
Log.i("info", "Exit Game");
System.exit(0);
}
对应变量定义为:
publicstaticfinalintBEGIN_ID = Menu.FIRST;
publicstaticfinalintPAUSE_ID=2;
publicstaticfinalintEXIT_ID=3;
@Override
publicvoidonCreate(Bundle icicle) {
super.onCreate(icicle);
Log.i("info", "NotePad Create");
setContentView(R.layout.main);
}
【android.util.Log 提供了方便的日志处理log.i(),log.e(),log.d()等】
运行程序:
点击菜单效果如下:
点击begin Game 相应日志如下:
点击exit Game
返回主程序
在前例子的onCreate方法中,创建的TextView从默认的main.xml中读取解析。
publicvoidonCreate(Bundle icicle) {
super.onCreate(icicle);
Log.i("info", "NotePad Create");
setContentView(R.layout.main);
}
此时展示的内容为:
Layout下的main.xml中的东西如:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="欢迎使用,Gpx Notepad Game..."/>
</LinearLayout>
根据setContentView开支持如下的参数:
void android.app.Activity.setContentView(View view)
Set the activity content to an explicit view.
参数为:View显示相应的内容.
所以采用setContentView的方法进行显示视图
通过自定义TextView
public TextView getContentTextView(String contentText){
android.widget.TextView tv=new TextView(this);
tv.setBackgroundColor(android.graphics.Color.YELLOW);
tv.setText(contentText);
return tv;
}
修改setContentView的调用方法
this.setContentView(getContentTextView("开始自定义Content Text View"));
运行效果为:
通过TextView 设置背景颜色【注意Android平台中,没有java.awt.Color,通过android.graphics.Color来替代】
针对点击不同的菜单,可以通过setContentView来调整需要现实的View.
publicvoid beginGame(){
Log.i("info", "Begin game ");
setContent("Begin Game...");
}
publicvoid pauseGame(){
Log.i("info", "Pause game");
setContent("Pause Game...");
}
调整视图的方法为:
publicvoid setContent(String contentInfo){
TextView tv=getContentTextView(contentInfo);
setContentView(tv);
}
【总结针对,菜单的不同动作,可以调整ContentView的显示视图,来完成对视图的调整处理操作】。