AIDE Android App基础教程_第十八课答案-Action Bar(操作栏)

第十八课,本课中您将学习添加项目到操作栏 ,运行此电子邮件应用程序

操作栏的内容可以像菜单一样用XML菜单资源文件进行定义

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

</menu>

我们增加了一个项目“Send”

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

	<item
        android:title="Send" />
        
</menu>

现在添加项目属性“android:showAsAction”为“ifRoom|withText”,这是告诉系统如果可能的话,显示操作栏,而不是菜单项

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

	<item
        android:showAsAction="ifRoom|withText"
        android:title="Send" />
        
</menu>

添加属性“android:icon”为“@android:drawable/ic_menu_send”,设置操作栏项目的图标

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

	<item
        android:icon="@android:drawable/ic_menu_send"
        android:showAsAction="ifRoom|withText"
        android:title="Send" />
        
</menu>

要创建操作栏,我们必须实现“onCreateOptionsMenu”方法,对于系统的操作栏和选项菜单都是一样的,只是项目显示不同

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.widget.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
    }
    
    @Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// TODO: Implement this method
		return super.onCreateOptionsMenu(menu);
	}
}

在“onCreateOptionsMenu”里我们使用菜单资源“R.menu.main_menu”填充操作栏

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.widget.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
    }
    
    @Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate main_menu.xml 
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return true;
	}
	
	
}

设置“Send”项的“android:id”属性为“@+id/mainMenuSend”

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

	<item
        android:id="@+id/mainMenuSend"
        android:icon="@android:drawable/ic_menu_send"
        android:showAsAction="ifRoom|withText"
        android:title="Send" />
        
</menu>

现在覆写“Activity”的方法“public boolean onOptionsItemSelected(MenuItem item)”,提示:只要在方法体输入“onOp”然后使用代码提示功能来完成

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.widget.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
    }
    
    @Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate main_menu.xml 
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return true;
	}

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }
	
	
}

使用if或switch语句检查“item.getItemId()”是否是“R.id.mainMenuSend”并使用“Toast.makeText(…)”显示提示“Sending…”

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.widget.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
    }
    
    @Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate main_menu.xml 
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		// TODO: Implement this method
		switch(item.getItemId()){
            case R.id.mainMenuSend:
                Toast.makeText(this,"Sending...",Toast.LENGTH_LONG).show();
                break;
        }
		return super.onOptionsItemSelected(item);
	}
}

欢迎加入AIDE教程网官方交流群:961607042

© 版权声明
THE END
喜欢就支持以下吧
点赞0赞赏
分享
评论 共1条

请登录后发表评论