PopupWindow的显示位置及简单的响应事件
2012-12-07 10:36
441 查看
这是点击菜单键 弹出的popupView
话不多说,直接上代码吧,部分代码参考了其他blog
public class PopupWinBean {
private static String[] info=new String[]{"1","2","3","4","5","6","7","8","10","11","12","13"
,"111","112","113","14","15","16","7","8","10","11","12","13"};
public static PopupWindow makeChangeWindow(Context context ){
View popupView = LayoutInflater.from(context).
inflate(R.layout.popup_gridview_change, null);
GridView gridView = (GridView) popupView.findViewById(R.id.select_grid);
gridView.setAdapter(new PlayChangeAdapter(context, info));
PopupWindow window = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT ,true);
//有这句代码 点击PopupWindow外部区域 PopupWindow会自动消失
window.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.ic_action_search));
return window;
}
public static PopupWindow makeWindow(final Context context ){
View popupView = LayoutInflater.from(context).
inflate(R.layout.popup_view_selectplay_item, null);
TextView textView = (TextView) popupView
.findViewById(R.id.select_text);
textView.setText("测试popupview显示位置");
textView.setTextColor(context.getResources().getColor(R.color.white));
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
((IRefreshPlayType)context).refreshCurrentPlayType("测试popupview显示位置");
}
});
PopupWindow window = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT ,true);
window.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.ic_action_search));
return window;
}
}
public class PopupWindowAc extends Activity implements OnClickListener,IRefreshPlayType {
PopupWindow pp;
Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.popupwindow);
mContext=this;
initView();
}
private void initView() {
// TODO Auto-generated method stub
findViewById(R.id.bt1).setOnClickListener(this);
findViewById(R.id.bt2).setOnClickListener(this);
findViewById(R.id.bt3).setOnClickListener(this);
findViewById(R.id.bt4).setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bt1:
pp=PopupWinBean.makeChangeWindow(mContext);
pp.showAsDropDown(findViewById(R.id.bt4));
break;
case R.id.bt2:
pp=PopupWinBean.makeWindow(mContext);
pp.showAtLocation(v, Gravity.TOP, 0, 45);
break;
case R.id.bt3:
pp=PopupWinBean.makeWindow(mContext);
pp.showAtLocation(v, Gravity.CENTER|Gravity.LEFT, 10, 0);
break;
case R.id.bt4:
pp=PopupWinBean.makeWindow(mContext);
pp.showAtLocation(v, Gravity.BOTTOM|Gravity.RIGHT, 0, 0);
break;
default:
break;
}
}
@Override
public void refreshCurrentPlayType(String playStr) {
// TODO Auto-generated method stub
ToastUtil.showToast(mContext, playStr);
pp.dismiss();
}
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
// TODO Auto-generated method stub
pp=PopupWinBean.makeChangeWindow(mContext);
pp.showAsDropDown(findViewById(R.id.bt1), 30, 0);
return false; // true--显示系统自带菜单;false--不显示。
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add("menu");
return super.onCreateOptionsMenu(menu);
}
}
//自定义接口
public interface IRefreshPlayType {
public void refreshCurrentPlayType(String playStr);
}
布局文件popupwindow.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" android:padding="15dip" >
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_gravity="top|right"
android:id="@+id/bt1" android:text="指定控件下部"
/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt2" android:text="相对屏幕置顶"
/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt3" android:text="相对屏幕靠左居中"
/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt4" android:text="相对屏幕靠右置底"
/>
</LinearLayout>
布局文件popup_gridview_change.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="right" >
<GridView android:id="@+id/select_grid"
android:background="@color/white" android:padding="3dip"
android:layout_width="60dip" android:layout_height="wrap_content"
android:gravity="center"
android:numColumns="1" />
</LinearLayout>
布局文件popup_view_selectplay_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:textSize="20sp"
android:textColor="@color/white" android:shadowDx="0.2"
android:layout_height="80dip" android:id="@+id/select_text"
android:background="@color/red" android:gravity="center" />
相关文章推荐
- Android基础教程(三)之-----简单的Button事件响应综合提示控件Toast的应用
- Popupwindow 显示, 其他背景变暗。 并加上点击事件 ~ (用于记录)
- ListView应用------ListView控件显示List集合中的数据,并对Item响应选择事件(法一)
- [C#] WinForm中按钮响应回车事件的简单方法
- Android PopupWindow显示位置和显示大小
- 自定义Popupwindow并指定显示位置!
- PopupWindow和dialog的显示位置
- PopupWindow在安卓7.0及7.1系统上位置显示异常
- C# 在WinForm中按钮响应回车事件的简单方法
- iOS开发之解决隐藏tabbar后原位置无法响应点击事件的问题
- 自定义Popupwindow并指定显示位置
- iOS开发(第三方使用)——百度地图的简单使用(定位与当前位置的显示)
- GTK学习笔记1-简单的控件和事件响应
- Popupwindow 显示, 其它背景变暗。 并加上点击事件 ~ (用于记录)
- Android入门教程(八)之-----简单的Button事件响应综合提示控件Toast的应用(转)
- ListView应用------ListView控件显示List集合中的数据,并对Item响应选择事件(法二)
- Android PopupWindow显示位置和显示大小
- C#WinForm中按钮响应回车事件的简单方法
- PopupWindow在Android7.0系统中显示位置错误
- 自适应空间的PopupWindow,动态修改显示位置(支持上下)