Android Api Demos登顶之路(四十八)Menu

来源:互联网 发布:cad网络电子版图纸 编辑:程序博客网 时间:2024/06/10 01:58

这个demo演示了menu的简单用法比较简单
res下创建menu目录,在menu目录中
main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    tools:context="com.fishtosky.menudemo.MainActivity" >    <item        android:id="@+id/browser_visibility"        android:title="Browser Visibility"/>    <group         android:id="@+id/browser">        <item             android:title="Refresh"            android:id="@+id/refresh">        </item>        <item             android:title="Bookmark"            android:id="@+id/book_mark"></item>    </group></menu>

title_only.xml

<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android" >    <item android:title="Jump"        android:id="@+id/jump"/>    <item android:title="Dive"        android:id="@+id/dive"/></menu>

MainActivity

public class MainActivity extends Activity {    private Spinner mSpinner;    private TextView mInstruction;    private static final int[] menuSampleResource = new int[] {            R.menu.title_only, R.menu.main };    private static final String[] menuNames = new String[] { "Title Only",            "Groups" };    private static final int SPINNER = 0x110;    private Menu mMenu;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        // 定义一个纯性布局        LinearLayout layout = new LinearLayout(this);        layout.setOrientation(LinearLayout.VERTICAL);        // 定义下拉列表的适配器        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,                android.R.layout.simple_spinner_item, menuNames);        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);        // 定义下拉列表        mSpinner = new Spinner(this);        mSpinner.setId(SPINNER);        mSpinner.setAdapter(adapter);        mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {            @Override            public void onItemSelected(AdapterView<?> parent, View view,                    int position, long id) {                // 重新构建menu                invalidateOptionsMenu();            }            @Override            public void onNothingSelected(AdapterView<?> parent) {                // 什么也不做            }        });        //将下拉列表添加到线性布局当中        layout.addView(mSpinner, new LayoutParams(LayoutParams.MATCH_PARENT,                LayoutParams.WRAP_CONTENT));        //定义并初始化textView        mInstruction = new TextView(this);        String text="Select a menu resource and press the menu key.";        mInstruction.setText(text);        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);        lp.setMargins(10, 10, 10, 10);        //将textView添加到布局中        layout.addView(mInstruction, lp);        //设置activity的视图        setContentView(layout);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        this.mMenu=menu;        //获取menu的填充器        MenuInflater inflater=getMenuInflater();        inflater.inflate(menuSampleResource[mSpinner.getSelectedItemPosition()], menu);        mInstruction        .setText("if you want to chose another menu resource,go back and re-run this activity");        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        switch (item.getItemId()) {        case R.id.jump:            Toast.makeText(this, "jump up to the air", 0).show();            return true;        case R.id.dive:            Toast.makeText(this, "Dive into the warter", 0).show();            return true;        case R.id.browser_visibility:            boolean shouldShowBrowser=!mMenu.findItem(R.id.refresh).isVisible();            mMenu.setGroupVisible(R.id.browser, shouldShowBrowser);            break;        default:            //如果当前的菜单项没有子菜单            if(!item.hasSubMenu()){                Toast.makeText(this, item.getTitle(), 0).show();                return true;            }            break;        }        return false;    }}
0 0
原创粉丝点击