您的位置:首页 > 运维架构

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" />

 

 

 

 

 

 

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Menu PopupWindow