PopupWindow弹窗
1:OptionsMenu
编写布局:
在res下面创建一个menu文件夹,并新建一个xml文件作为OptionMenu的布局文件
重写创建方法:
Activity重写onCreateOptionsMenu加载资源文件
添加事件监听:
Activity重写onOptionsItemSelected加设置事件监听
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="" target="_blank">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>
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options_menu,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options_menu,menu);
return super.onCreateOptionsMenu(menu);
}
2:ContextMenu
1.在res下面创建一个menu文件夹,并新建一个xml文件作为ContextMenu的布局文件,我们复用上面的menu布局
2.Activity重写onCreateConextMenu加载资源文件
3.Activity重写onConextItemSelected设置事件监听
4.为控件添加长按属性并将菜单绑定到这个控件上:registerForContextMenu(控件)
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);
}
3:PopupMenu
步骤1:在res下面创建一个menu文件夹,并新建一个xml文件作为PoupMenu的布局文件。
步骤2:把PopupMenu相关逻辑封装到showPopupMenu()方法中,包含PopupMenu的实例化、布局设置、显示、添加MenuItem的点击监听及响应等
步骤3:为控件设置事件监听直接调用showPopupMenu()方法
<?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>
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(); }}
4:PopupWindow弹窗
定义:PopupWindow在当前页面显示任意视图并可以指定显示位置的弹出窗口。
PopupWindow mPopupWindow=new PopupWindow(PopouowindowActivity.this);
View popView=inflater.inflate(R.layout.popupwindow_layout, null);
mPopupWindow.showAtLocation(btn8,Gravity.BOTTOM,0,0);
WindowManager.LayoutParams lp=getWindow().getAttributes();
lp.alpha=0.5f;
getWindow().setAttributes(lp);
mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
public void onDismiss() {
WindowManager.LayoutParams lp=getWindow().getAttributes();
lp.alpha=1.0f;
}
});
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode==KeyEvent.KEYCODE_BACK){
mPopupWindow.dismiss();
return true;
}
}
public class DIYPopupWindow extends PopupWindow {
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mContentView = mInflater.inflate(R.layout.diy_popupwindow,null);
setContentView(mContentView);
<style name="MyPopupWindow">
<item name="android:windowEnterAnimation">@anim/pop_in</item>
<item name="android:windowExitAnimation">@anim/pop_out</item>
</style>
DIYPopupWindow popupWindow = new DIYPopupWindow(this);
popupWindow.showAtLocation(button,Gravity.BOTTOM,0,0);
Popupwindow在显示之前一定要设置宽高,Dialog无此限制
Popupwindow默认不会响应物理键盘的back,除非显示设置了popup.setFocusable(true);而在点击back的时候,Dialog会消失
Popupwindow不会给页面其他的部分添加蒙层,而Dialog会
Popupwindow没有标题,Dialog默认有标题,可以取消标题
二者显示的时候都要设置Gravity。如果不设置,Dialog默认是Gravity.CENTER
二者都有默认的背景,都可以通过setBackgroundDrawable(new ColorDrawable(android.R.color.transparent));去掉