您的位置:首页 > 移动开发 > Android开发

Android 程式开发:(九)使用活动栏 —— 9.3自定义ActionBar项目和程序图标

2012-04-24 21:45 627 查看
在上一节中,菜单选项是没有文字的。如果想要同时显示图标和文字,可以一起使用“|”和MenuItem.SHOW_AS_ACTION_WITH_TEXT常量。

[java]
view plaincopyprint?

MenuItem mnu1 = menu.add(0,0,
0,"Item 1");

{
mnu1.setIcon(R.drawable.ic_launcher);
mnu1.setShowAsAction(
MenuItem.SHOW_AS_ACTION_IF_ROOM |
MenuItem.SHOW_AS_ACTION_WITH_TEXT);
}

[java] 
view plaincopyprint?

private boolean MenuChoice(MenuItem item) 
    {          
        switch (item.getItemId()) { 
        case  android.R.id.home:  

            Toast.makeText(this,  
                "You clicked on the Application icon",  
                Toast.LENGTH_LONG).show();  
            return true; 
        case 0: 
            Toast.makeText(this, 
"You clicked on Item 1",   
                Toast.LENGTH_LONG).show();  
            return true; 
        case 1: 
           ......  
    }      

private boolean MenuChoice(MenuItem item)
    {        
        switch (item.getItemId()) {
        case  android.R.id.home:
            Toast.makeText(this, 
                "You clicked on the Application icon", 
                Toast.LENGTH_LONG).show();
            return true;
        case 0:
            Toast.makeText(this, "You clicked on Item 1", 
                Toast.LENGTH_LONG).show();
            return true;
        case 1:
           ......
    }
如果想让应用图标能被点击,我们需要调用setDisplayHomeAsUpEnable()方法:

[java]
view plaincopyprint?

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
//actionBar.hide();
//actionBar.show(); //---show it again---
}

[java] 
view plaincopyprint?

case  android.R.id.home: 
    Toast.makeText(this,   
        "You clicked on the Application icon",  
        Toast.LENGTH_LONG).show();  
  
    Intent i = new Intent(this, MyActionBarActivity.class); 
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
    startActivity(i);  
  
    return true; 

        case  android.R.id.home:
            Toast.makeText(this, 
                "You clicked on the Application icon", 
                Toast.LENGTH_LONG).show();

            Intent i = new Intent(this, MyActionBarActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(i);

            return true;
使用Intent.FLAG_ACTIVITY_CLEAR_TOP标识,可以确保当点击应用图标的时候,在“返回栈”里面的那些activity都会被清除掉。使用这种方法,如果用户点击返回键,其他的activity将不会再显示出来。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐