Android 菜单开发和视图处理

xiaoxiao2026-05-28  19

自己动手新增一个工程   技巧:默认创建一个类, R.java 不要修改,将 Nodepadv2.java 中对 R import 路径删除,重新通过 Ctrl+shift+O 导入,选择 org.huareal.demo.R[ 具体随不同项目而定 ]   如果想改变R.java中的内容,修正res目录下的内容的 其中strings.xml内容修改为 <?xml version= "1.0" encoding= "utf-8" ?> <resources>     <string name= "app_name" > GGame </string>     <string name= "menu_begin" > begin Game </string>     <string name= "menu_pause" > pause Game </string>     <string name= "menu_exit" > exit Game </string> </resources> 其中的menu_begin menu_pause,menu_exit都属于后续添加的。为了使R.java具有上述内容,通过选择project中clean操作,清理对应的工程,R.java将自动更新。   1     增加菜单   该实例中,首先根据练习1中,增加相应的三个菜单,分别对应上述的三个menu 为了显示菜单,通过实现方法: @Override     public boolean 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     public boolean onOptionsItemSelected(Item item) {         Log.i ( "info" , "NotePad Menu select " );             switch (item.getId()) {         case BEGIN_ID :             beginGame();                 break ;         case PAUSE_ID :              pauseGame();              break ;         case EXIT_ID :              exitGame();              break ;         default :              Log.i ( "i" , " 无效的菜单选项 " );              break ;         }                 return super .onOptionsItemSelected(item);     }         public void beginGame(){     Log.i ( "info" , "Begin game " );     }     public void pauseGame(){     Log.i ( "info" , "Pause game" );     }     public void exitGame(){         Log.i ( "info" , "Exit Game" ); System.exit(0); } 对应变量定义为: public static final int BEGIN_ID = Menu. FIRST ;     public static final int PAUSE_ID =2;     public static final int EXIT_ID =3;         @Override     public void onCreate (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 返回主程序   2     修正显示视图 在前例子的onCreate方法中,创建的TextView从默认的main.xml中读取解析。 public void onCreate (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. public void beginGame(){     Log.i ( "info" , "Begin game " );     setContent( "Begin Game..." ); } public void pauseGame(){     Log.i ( "info" , "Pause game" );     setContent( "Pause Game..." ); } 调整视图的方法为: public void setContent(String contentInfo){     TextView tv=getContentTextView(contentInfo);     setContentView(tv); } 【总结针对,菜单的不同动作,可以调整 ContentView 的显示视图,来完成对视图的调整处理操作】。 相关资源:android 底部弹出菜单(带透明背景)
转载请注明原文地址: https://www.6miu.com/read-5049543.html

最新回复(0)