Android创建下拉菜单

时间:2012-03-20 19:04:19

标签: android menu

有谁知道如何创建某种下拉或弹出页面,以便我可以从我所在的页面转到任何页面?

例如我有一个菜单页面,但当我选择一个选项时,我希望能够导航到另一个页面而不是返回菜单

public class PopUpMenu extends Activity {


 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.test);
 }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.popupmenu, menu);
    return true;



}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.location:
        Intent intent1 = new Intent(getApplicationContext(), MyLocation.class);
        startActivity(intent1);
        return true;
    case R.id.search:
        Intent intent2 = new Intent(getApplicationContext(), Search.class);
        startActivity(intent2);
        return true;
    case R.id.add:
        Intent intent3 = new Intent(getApplicationContext(), AddSite.class);
        startActivity(intent3);
        return true;
//      case R.id.help:
//          Intent intent4 = new Intent(getApplicationContext(), Help.class);
//          startActivity(intent4);
//          return true;    
    case R.id.exit:    
        Intent intent5 = new Intent(Intent.ACTION_MAIN);
        intent5.addCategory(Intent.CATEGORY_HOME);
        intent5.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent5);
    default:
        return super.onOptionsItemSelected(item);
    }

}



<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/location" android:title="My Location" />
<item android:id="@+id/search" android:title="Search" />
<item android:id="@+id/add" android:title="Add Site" />
<item android:id="@+id/help" android:title="Help" />
<item android:id="@+id/exit" android:title="Exit" />
</menu>

2 个答案:

答案 0 :(得分:2)

首先,这是一个很好的教程:Activity Testing

您在developers.android网站上有几个示例 最全面的是API Demos,其中包括广泛的工具和技术。

然后,为了您的目的,窗口小部件名为Spinner,其演示版为Spinner Test

答案 1 :(得分:1)

菜单(XML格式):

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/food" android:title="Food" />
<item android:id="@+id/other" android:title="Other" />
</menu>

设置菜单:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.mymenu, menu);
    return true;
}

处理转换活动:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.food:
        Intent intent1 = new Intent(getContext(), Food.class);
        startActivity(intent1);
        return true;
    case R.id.other:
        Intent intent2 = new Intent(getContext(), Other.class);
        startActivity(intent2);
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }

}