父Activity 里面的 布局 的 最上层的 layout 需要添加一个 ID : android:id="@+id/main"

package t1.mft;import android.app.Activity;import android.os.Bundle;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.WindowManager;import android.widget.Button;import android.widget.PopupWindow;public class T1Activity extends Activity {    /** Called when the activity is first created. */private LayoutInflater inflater; private View layout;private PopupWindow menu; private Button button1;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        button1 = (Button)findViewById(R.id.button1);        initMenu();        button1.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubshow();}});    }    //实例化PopupWindow创建菜单      private void initMenu(){                //获取LayoutInflater实例              inflater  = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);              //获取弹出菜单的布局              layout = inflater.inflate(R.layout.menu,null);             final Button close = (Button)layout.findViewById(R.id.close);                         //关闭Pop            close.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubmenu.dismiss();}});                        //设置popupWindow的布局              menu = new PopupWindow(layout, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT);        }        //显示菜单      private void show(){             //设置位置              menu.showAtLocation(T1Activity.this.findViewById(R.id.main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL,0,0); //设置在屏幕中的显示位置              menu.setFocusable(true); //设置PopupWindow可获得焦点            menu.setTouchable(true); //设置PopupWindow可触摸            menu.setOutsideTouchable(false); //设置非PopupWindow区域可触摸    }         }

附带上衣个弹出框的样式

<?xml version="1.0" encoding="utf-8"?><RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:gravity="center_horizontal"    android:orientation="vertical"  ><LinearLayout     android:id="@+id/pop_layout"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:gravity="center_horizontal"    android:orientation="vertical"    android:layout_alignParentBottom="true"     android:background="@drawable/btn_style_alert_dialog_background"     ></LinearLayout></RelativeLayout>

其中btn_style_alert_dialog_background 如下图



如果要实现 父级 布局被遮盖 灰掉,

popAlert = new PopupWindow(alert_layout, WindowManager.LayoutParams.FILL_PARENT,WindowManager.LayoutParams.FILL_PARENT);
然后弹出POP的布局文件 的 最 定居的布局文件 可以加载一个 透明的背景图片

<?xml version="1.0" encoding="utf-8"?><RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="@drawable/alpha_50"    android:gravity="center_horizontal"    android:orientation="vertical"  >



更多相关文章

  1. android EditText实例 删除按钮 与内容监听
  2. 动态广告布局
  3. 设置android的布局文件的背景颜色为黑色
  4. Android 属性动画实现的扇形菜单效果
  5. android animation的应用实例
  6. 相对布局 relativelayout
  7. Android从服务器获取图片的实例方法
  8. Android SlidingMenu 开源项目 侧拉菜单的使用(详细配置)
  9. Android 约束布局(ConstraintLayout)1.1.0 版详解

随机推荐

  1. Android(安卓)SDK版本更新
  2. 修改系统Android版本,版本号
  3. 系出名门Android(8) - 控件(View)之TextS
  4. Android也有beacon了
  5. android adb工具
  6. Android微信界面
  7. 关于Chromium的Android(安卓)Studio使用
  8. android图像处理系统1.3
  9. How To Open An URL In Android’s Web B
  10. 详解Android中的屏幕方向