第二单元 Menu菜单及PopupWindow弹窗
系统菜单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">
<item android:id="@+id/blue" android:title="蓝色" app:showAsAction="never"></item>
<item android:id="@+id/red" android:title="红色" app:showAsAction="never"></item>
<item android:id="@+id/green" android:title="绿色" app:showAsAction="never"></item>
</menu>
2.Activity重写onCreateOptionsMenu加载资源文件
3.Activity重写onOptionsItemSelected加设置事件监听
TextView textView;
//重写onCreteOptionsMenu加载资源文件
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
return super.onCreateOptionsMenu(menu);
}
//重写onOptionsTtemSelected 设置事件监听
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.blue:{
textView.setTextColor(Color.parseColor("#FF0695F1"));
break;
}
case R.id.red:{
textView.setTextColor(Color.parseColor("#FFF50606"));
break;
}
case R.id.green:{
textView.setTextColor(Color.parseColor("#FF4DB051"));
break;
}
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activitya);
textView = findViewById(R.id.text);
}
上下文菜单ContextMenu
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activitya);
textView = findViewById(R.id.text);
//为控件添加长按属性
registerForContextMenu(textView);
}
//重写onCreateContextMenu加载资源文件
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
getMenuInflater().inflate(R.menu.menu,menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
//重写onContextItemSelected 设置事件监听
@Override
public boolean onContextItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.blue:{
textView.setTextColor(Color.parseColor("#FF0695F1"));
break;
}
case R.id.red:{
textView.setTextColor(Color.parseColor("#FFF50606"));
break;
}
case R.id.green:{
textView.setTextColor(Color.parseColor("#FF4DB051"));
break;
}
}
return super.onContextItemSelected(item);
}
PopupWindow
PopupWindow可以实现和dialog相似的弹出框效果,但是PopupWindow的特点是定位更准确、更灵活,宽高和边界都比较清晰。
内涵
1:可以指定显示位置
2:可用于显示任意视图
3:出现在当前页面
定义
PopupWindow在当前页面显示任意视图并可以指定显示位置的弹出窗口
PopupWindow的方法
setWidth()/setHeight() ———— 设置PopupWindow的宽高
setContentView() ———— 设置PopupWindow显示的视图
↑上面是必须的方法
showAtLocation() ———— 显示在指定位置
setBackgroundDrawable() —— 设置PopupWindow的背景
setOutsideTouchable() ———— 设置PopupWindow外部可点击
showAsDropDown() ———— 设置PopupWindow显示在某个控件的下方
实例化PopupWindow ,设置必要的方法
PopupWindow mPopupWindow=new PopupWindow(PopouowindowActivity.this);
View popView=inflater.inflate(R.layout.popupwindow_layout, null);
mPopupWindow.showAtLocation(btn8,Gravity.BOTTOM,0,0);
showAtLocation():显示的位置
Gravity.BOTTOM:显示在页面底部
PopupWindow弹出时,添加半透明背景
WindowManager.LayoutParams lp=getWindow().getAttributes();
lp.alpha=0.5f;
getWindow().setAttributes(lp);
alpha:透明度 范围:0-1,由完全透明到不透明
关闭时的监听事件
mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
public void onDismiss() {
WindowManager.LayoutParams lp=getWindow().getAttributes();
lp.alpha=1.0f;
}
});
如何自定义窗体
(1)构造方法:public PopupWindow (Context context):context上下文对象
(2)必须设置的3大要素:
setContentView():设置自定义布局
setWidth():设置宽度
setHeight():设置高度
(3)显示窗体:
a。显示在某个指定控件的下方
showAsDropDown(View anchor):
showAsDropDown(View anchor, int xoff, int yoff);//xoff和yoff都是偏移量
b。指定父视图,显示在父控件的某个位置(Gravity.TOP,Gravity.RIGHT等)
showAtLocation(View parent, int gravity, int x, int y);
//gravity可以是Gravity.TOP、Gravity.BOTTOM、Gravity.LEFT、Gravity.RIGHT
实现微信QQ支付宝右上角加号弹出窗体
XML代码
item代码
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加好友"
android:textColor="@android:color/white"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="扫一扫"
android:textColor="@android:color/white"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="创建群聊"
android:textColor="@android:color/white"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="面对面快传"
android:textColor="@android:color/white"/>
</LinearLayout>
java代码
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.text1);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final PopupWindow popupWindow = new PopupWindow(MainActivity.this);
//设置属性
popupWindow.setWidth(150);
popupWindow.setHeight(300);
final View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);
popupWindow.setContentView(view);
//设置点击外部消失
popupWindow.setOutsideTouchable(true);
final WindowManager.LayoutParams attributes = getWindow().getAttributes();
attributes.alpha = 0.5f;
getWindow().setAttributes(attributes);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {//消失监听
@Override
public void onDismiss() {
attributes.alpha = 1f;//消失后透明度调回去
getWindow().setAttributes(attributes);//设置回去
}
});
popupWindow.showAsDropDown(textView);//展示在什么控件下面
}
});
}
在父页面下方弹出
java代码
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.text1);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final PopupWindow popupWindow = new PopupWindow(MainActivity.this);
//设置属性
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
final View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);
popupWindow.setContentView(view);
//设置点击外部消失
popupWindow.setOutsideTouchable(true);
final WindowManager.LayoutParams attributes = getWindow().getAttributes();
attributes.alpha = 0.5f;
getWindow().setAttributes(attributes);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {//消失监听
@Override
public void onDismiss() {
attributes.alpha = 1f;//消失后透明度调回去
getWindow().setAttributes(attributes);//设置回去
}
});
//展示
/**
* parent 父布局
* gravity 相对于父布局哪
* x x轴偏移量
* y y轴偏移量
*/
popupWindow.showAtLocation(view,Gravity.BOTTOM,0,0);//在主页面下方弹出
}
});
}