欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android中的菜单

程序员文章站 2022-05-26 20:36:40
...

***Android中的菜单

*OptionsMenu:
onCreatOptiodMenu()方法

首先在menu文件夹下创建options_menu.xml文件:

<item 
    android:id="@+id/action_search"
    android:title="@string/action_search"/>
<item 
    android:id="@+id/action_settings"
    android:title="@string/action_settings"/>

MainActivity.java中:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    //创建选项菜单--OptionsMenu
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //获取菜单填充器对象
        MenuInflater menuinflater = getMenuInflater();
        //将menu的xml文件转换成menu对象
        menuinflater.inflate(R.menu.options_menu, menu);
        //也可以通过代码形式添加菜单项
        menu.add(Menu.NONE, Menu.NONE,Menu.NONE, "扫描");
        //返回必须是true,否则不显示menu
        return true;
    }
    //处理OptionMenu的点击事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.action_search:
            Toast.makeText(MainActivity.this, "搜索", Toast.LENGTH_SHORT).show();
            break;
        case R.id.action_settings:
            Toast.makeText(MainActivity.this, "设置", Toast.LENGTH_SHORT).show();
        default:
            break;
        }
        return super.onOptionsItemSelected(item);   
    }
}

*ContextMenu:
onCreatContextMenu()方法

案例效果:需长按textview控件才可显示出菜单,通过选择相应的菜单项,在title中显示控件宽高

在activity_main.xml布局文件中添加一个textView控件,values文件夹下的main.xml文件中:

<item
    android:id="@+id/action_height"
    android:title="@string/action_height"/>
<item 
    android:id="@+id/action_width"
    android:title="@string/action_width"/>

MainActivity.java中:

public class MainActivity extends Activity {
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        tv = (TextView) findViewById(R.id.tv);
        registerForContextMenu(tv);//必须注册上下文菜单-->通常上下文菜单是和ListView或者GridView这种适配器控件绑定在一起
    }
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        MenuInflater menuinflater = getMenuInflater();
        menuinflater.inflate(R.menu.main, menu);
        
        super.onCreateContextMenu(menu, v, menuInfo);
    }
    
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.action_height:
            setTitle("控件的高度="+tv.getHeight());
            break;
        case R.id.action_width:
            setTitle("控件的宽度="+tv.getWidth());
        default:
            break;
        }
        return super.onContextItemSelected(item);
    }
}

Android中的菜单

*PopupMenu:
PopupMenu popMenu = new PopupMenu(this,v)

案例显示效果:通过点击button按钮显示出菜单选项,通过选择相应的菜单选项设置textview中字体的增大和减小

activity_main.xml中布局:

<Button 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="showPopup"
    android:textColor="#fff"
    android:background="#90C552"
    android:text="显示PopupMenu"/>

<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="@string/hello_world" />

menu文件夹下的main.xml中设置菜单选项:

<item
    android:id="@+id/action_textadd"
    android:title="@string/action_textadd"/>
<item 
    android:id="@+id/action_textsub"
    android:title="@string/action_textsub"/>

MainActivity.java文件中:

public class MainActivity extends Activity {
    private float fontSize =15.0f;
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tv);
    }

    public void showPopup(View view){
        PopupMenu popupmenu = new PopupMenu(this, view);//第二个参数anchor:锚点,popupmenu想要显示出来必须与一个控件绑定在一起,所以这里将其与view绑定,view也就是button
        getMenuInflater().inflate(R.menu.main, popupmenu.getMenu());
        popupmenu.show();
        //处理菜单选项点击事件
        popupmenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
            
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                case R.id.action_textadd:
                    fontSize+=5;
                    break;
                case R.id.action_textsub:
                    fontSize-=5;
                default:
                    break;
                }
                tv.setTextSize(fontSize);
                return false;
            }
        });
    }
}

Android中的菜单