目录
OptionsMenu
效果图
菜单类型
代码实现步骤
1、在res下面创建一个menu文件夹,并新建一个xml文件作为OptionMenu的布局文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!--showAsAction属性:always总是 never 从不 ifroom 能显示就显示 默认是nerver-->
<item android:id="@+id/blue" android:title="蓝色" app:showAsAction="never" ></item>
<item android:id="@+id/red" android:title="红色" > </item>
<item android:id="@+id/green" android:title="绿色" ></item>
</menu>
2、在当前类里重写onCreateOptionMenu加载资源文件
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options_menu,menu);
return super.onCreateOptionsMenu(menu);
}
3、在当前类重写onOptionsltemSelected设置事件监听
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id=item.getItemId();
switch (id){
case R.id.blue:
textView.setTextColor(Color.parseColor("#2239A2"));
break;
case R.id.green:
textView.setTextColor(Color.parseColor("#1BA233"));
break;
case R.id.red:
textView.setTextColor(Color.parseColor("#A21C31"));
break;
}
return super.onOptionsItemSelected(item);
}
ContextMenu(上下文菜单)
代码实现步骤
public class MainActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.text);
//TODO 为控件添加长按属性并将菜单绑定到这个控件上
registerForContextMenu(textView);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
getMenuInflater().inflate(R.menu.options_menu, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.blue:
textView.setTextColor(Color.parseColor("#2239A2"));
break;
case R.id.green:
textView.setTextColor(Color.parseColor("#1BA233"));
break;
case R.id.red:
textView.setTextColor(Color.parseColor("#A21C31"));
break;
}
return super.onContextItemSelected(item);
}
PopupMenu(弹出菜单)
弹出菜单—实现思路
xml布局文件:activity_main2.xml(随便起)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main2Activity"
android:gravity="center">
<TextView
android:id="@+id/popup_tv"
android:text="弹出菜单"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Java代码:Main2Acticity.java
public class Main2Activity extends AppCompatActivity {
private TextView view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
view = (TextView) findViewById(R.id.popup_tv);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPopupMenu();
}
});
}
//TODO 弹出菜单
public void showPopupMenu(){
//TODO 1:创建对象
//参数一 上下文 参数二 菜单显示在指定控件的下方
PopupMenu popupMenu= new PopupMenu(this,view);
//TODO 2:记载布局
popupMenu.inflate(R.menu.options_menu);
//TODO 3:事件监听
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
int id=item.getItemId();
switch (id){
case R.id.blue:
view.setTextColor(Color.parseColor("#2239A2"));
break;
case R.id.green:
view.setTextColor(Color.parseColor("#1BA233"));
break;
case R.id.red:
view.setTextColor(Color.parseColor("#A21C31"));
break;
}
return false; } });
//TODO 4:显示
popupMenu.show(); }}
PopupWindow弹窗
定义
PopupWindow在当前页面显示任意视图并可以指定显示位置的弹出窗口。
常用方法
setWidth()/setHeight() 设置PopuWindow的宽高(必须要设置不然软件会报错)
showAtLocation() 显示在指定位置
setContentView() 设置PopuWindow的显示视图
setBackgroundDrawable() 设置PopupWindow的背景
setOutsideTouchable() 设置PopupWindow外部可点击(True是点击外部取消 false是点击外部删除)
showAsDropDown() 设置PopupWindow显示在某个控件的下方
实例化PopupWindow,设置必要的方法
PopuWindow弹出时,添加半透明背景
关闭时的监听事件
mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
public void onDismiss() {
WindowManager.LayoutParams lp=getWindow().getAttributes();
lp.alpha=1.0f;
}
});
重写onKeyDown方法,关闭PopuWindow
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode==KeyEvent.KEYCODE_BACK){
mPopupWindow.dismiss();
return true;
}
}