zoukankan      html  css  js  c++  java
  • 实验5 数独游戏界面设计

    实验5 数独游戏界面设计

    【目的】

      实现数独游戏的完整界面设计

    【要求】

      1、掌握ActionBar的使用;

    【原理】

      1)  使用ActionBar显示OptionMenu的菜单项MenuItem

      2)  使用程序图标导航

      3)  添加Action View

    【过程】  

          1、显示ActionBar

        在运行时通过调用show()来显示ActionBar。

            ActionBar actionBar = getActionBar(); 

            actionBar.show();

      2、使用ActionBar显示OptionMenu的菜单项MenuItem

        2.1 修改选项菜单文件main.xml

    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
        <item
            android:id="@+id/action_settings"
            android:orderInCategory="100"
            android:showAsAction="never"
            android:title="@string/app_name"/>
        <item 
            android:id="@+id/help" 
            android:title="帮助"
            android:showAsAction="always"/>
        <item 
            android:id="@+id/about" 
            android:title="关于"
            android:showAsAction="always"/>
        <item 
            android:id="@+id/clock" 
            android:title="时钟"
            android:showAsAction="always"
            android:actionLayout="@layout/clock"/>
    </menu>

        2.2将选项菜单资源文件中的每个<item.../>元素增加     

             android:showAsAction="always"属性
        2.3在Activity类中添加和重写以下

    public boolean onCreateOptionsMenu(Menu menu) {
            // TODO Auto-generated method stub
            MenuInflater inflater = new MenuInflater(this);
            inflater.inflate(R.menu.main, menu);
            return super.onCreateOptionsMenu(menu);
        }
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // TODO Auto-generated method stub
            switch (item.getItemId()) {
            case android.R.id.home:
                //创建启动MainActivity的Intent
                Intent intent=new Intent(this,MainActivity.class);
                //添加额外的Flag,将Activity栈中处于MainActivity之上的Activity弹出
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                break;
            case R.id.help:
                Intent intent1=new Intent(this,HelpActivity.class);
                startActivity(intent1);
                break;
            case R.id.about:
                Intent intent2=new Intent(this,AboutActivity.class);
                startActivity(intent2);
                break;    
            default:
                break;
            }
            return super.onOptionsItemSelected(item);
        }

      3、运行程序,查看结果。

          

       4、使用程序图标导航

        在protected void onCreate(Bundle savedInstanceState)方法中添加以下代码,对ActionBar初始化设置:

        ActionBar actionBar = getActionBar();//获取ActionBar对象
            actionBar.setDisplayShowHomeEnabled(true);//显示应用程序图标
            actionBar.setDisplayHomeAsUpEnabled(true);//将应用程序图标转变为可点击图标,并添加一个返回箭头。

          实现点击程序图标后返回到上一个页面(程序图标的ID默认为Android.R.id.home)

    @Override
        Public Boolean onOptionsItemSelected(MenuItem item) {
            // TODO Auto-generated method stub
            switch (item.getItemId()) {
            case android.R.id.home:
                //创建启动MainActivity的Intent
                Intent intent=new Intent(this,MainActivity.class);
                //添加额外的Flag,将Activity栈中处于MainActivity之上的Activity弹出
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                break;
    
            default:
                break;
            }
            returnsuper.onOptionsItemSelected(item);
        }

        运行结果图:

      

        5、添加Action View

               ActionBar除了可以显示普通的ActionItem之外,还可以显示普通的UI控件,如在ActionBar上显示一个时钟。

        方法:定义Action Item时使用android:actionLayout="@layout/clock"属性指定ActionView对应的视图布局资源。

         在layout文件夹中新建一个显示时钟的布局文件clock.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <AnalogClock
            android:id="@+id/analogClock1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
    </LinearLayout>

            在菜单资源文件main.xml中添加Action Item

    <item
    android:id="@+id/item3"
    android:actionLayout="@layout/clock"
    android:showAsAction="always"
    android:title="时钟">
    </item>

          查看效果

     【实验小结】

         通过这次实验实现了数独游戏的完整界面设计,掌握了ActionBar的使用。

  • 相关阅读:
    php7.4 降级 php7.1 的坑
    python 记录网页 生成pdf
    Mac 安装常用软件环境
    python 2.7 操作 excel
    007整数反转
    006Z字形变换
    005最长回文子串
    004寻找两个正序数组的中位数
    003无重复字符的最长子串
    002两数相加
  • 原文地址:https://www.cnblogs.com/jun-28blog/p/5394104.html
Copyright © 2011-2022 走看看