仓廪实则知礼节,衣食足则知荣辱。这篇文章主要讲述Android中的菜单相关的知识,希望能为你提供帮助。
***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);
}
}
文章图片
*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中的菜单】
文章图片
推荐阅读
- Android Studio Git 分支使用实践
- 想让安卓app不再卡顿(看这篇文章就够了)
- android recycleView 简单使用二---分割线
- Android实用代码
- 移动 App 接入 QQ 登录/分享流程
- Ionic3 填坑记录 - java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.Dex
- 微信小程序商城开源项目,Weixin-App-Shop 1.0 版本正式发布!!!
- 升级完Android Studio3.2后,打包release出现的错误
- android RecyclerView简单的使用