PopupWindow的显示位置及简单的响应事件
来源:互联网 发布:c语言三个整数排序 编辑:程序博客网 时间:2024/05/21 19:43
这是点击菜单键 弹出的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" />
- PopupWindow的显示位置及简单的响应事件
- Android PopupWindow 的显示位置
- Android PopupWindow 的显示位置
- Android PopupWindow的简单显示
- popupWindow 指定位置上的显示
- popupWindow 在指定位置上的显示
- PopupWindow 显示位置 大小的设置
- popupWindow 在指定位置上的显示
- popupwindow 显示在指定的位置
- popupWindow 在指定位置上的显示
- popupWindow 在指定位置上的显示
- PopupWindow和dialog的显示位置
- popupWindow 在指定位置上的显示
- Android7.0 popupWindow显示位置的问题
- popupWindow显示位置的探究以及canvas的起始位置
- 在popupWindow里响应硬件的back按键的事件
- popupWindow显示在指定位置上的显示
- 点击popupwindow外部区域,点击事件的响应问题
- python编码问题
- MySQL 查询缓存的实际应用代码示例
- Weblogic 9.2 LINUX安装方法
- GDI+ 添加图片资源函数封装
- JTAG接口20针、14针、8针的制作及重要信号定义
- PopupWindow的显示位置及简单的响应事件
- 转载:FastDFS的配置、部署与API使用解读(3)以流的方式上传文件的客户端代码
- <Android> Failed to pull selection 解决
- 越想越气(拍拍购物过程)
- 百度随心听私人频道
- 从配置文件里读取数据,实现多数据库连接
- 转载:FastDFS的配置、部署与API使用解读(4)FastDFS配置详解之Client配置
- Grails Domain自动验证的i18n
- dom4j(合并转载文章)