PopupWindow底部弹出

来源:互联网 发布:战舰世界格拉斯数据 编辑:程序博客网 时间:2024/04/30 22:15

参考:http://m.blog.csdn.net/blog/lowprofile_coding/47785421#comment


说明:从屏幕底部弹出PopupWindow,有弹出隐藏动画效果.背景设置透明度.

效果如下:


1.MainActivity.java   显示popwindow,宽高跟屏幕大小一样,设置一个透明度背景

public class MainActivity extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViewById(R.id.button).setOnClickListener(new OnClickListener() {public void onClick(View v) {showPopwindow();}});}private void showPopwindow() {View popView = View.inflate(this, R.layout.camera_pop_menu, null);Button btnCamera = (Button) popView.findViewById(R.id.btn_camera_pop_camera);Button btnAlbum = (Button) popView.findViewById(R.id.btn_camera_pop_album);Button btnCancel = (Button) popView.findViewById(R.id.btn_camera_pop_cancel);int width = getResources().getDisplayMetrics().widthPixels;int height = getResources().getDisplayMetrics().heightPixels;final PopupWindow popWindow = new PopupWindow(popView,width,height);popWindow.setAnimationStyle(R.style.AnimBottom);popWindow.setFocusable(true);popWindow.setOutsideTouchable(false);// 设置允许在外点击消失OnClickListener listener = new OnClickListener() {public void onClick(View v) {switch (v.getId()) {case R.id.btn_camera_pop_camera:break;case R.id.btn_camera_pop_album:break;case R.id.btn_camera_pop_cancel:break;}popWindow.dismiss();}};btnCamera.setOnClickListener(listener);btnAlbum.setOnClickListener(listener);btnCancel.setOnClickListener(listener);ColorDrawable dw = new ColorDrawable(0x30000000);popWindow.setBackgroundDrawable(dw);
<pre name="code" class="java" style="border: 1px solid rgb(255, 255, 204); font-family: 'Courier New'; overflow: auto; font-size: 16px; line-height: 24px; background-color: rgb(255, 255, 252);">                View parent = ((ViewGroup) this.findViewById(android.R.id.content)).getChildAt(0);
popWindow.showAtLocation(parent, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);}}

2.camera_pop_menu.xml   Popupwindow加载的布局文件

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="fill_parent"    android:orientation="vertical" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_margin="10dp"        android:orientation="vertical" >        <Button            android:id="@+id/btn_camera_pop_camera"            style="@style/txt_camera_pop_menu"            android:layout_width="match_parent"            android:layout_height="45dp"            android:background="@drawable/pop_first_selector"            android:text="@string/camera_pop_camera"            android:textSize="18sp" />        <Button            android:id="@+id/btn_camera_pop_album"            style="@style/txt_camera_pop_menu"            android:layout_width="match_parent"            android:layout_height="45dp"            android:background="@drawable/pop_last_selector"            android:text="@string/camera_pop_album"            android:textSize="18sp" />        <Button            android:id="@+id/btn_camera_pop_cancel"            style="@style/txt_camera_pop_menu"            android:layout_width="match_parent"            android:layout_height="45dp"            android:layout_marginTop="10dp"            android:background="@drawable/pop_single_selector"            android:text="@string/camera_pop_cancel"            android:textSize="18sp" />    </LinearLayout></RelativeLayout>
代码下载地址:http://download.csdn.net/detail/lowprofile_coding/9025369


笔者注:①.这里的popupwindow要注意定位时的控件,即parent--是系统提供的。

              ②.这个popupwindow不能当下拉列表使用,目前只能只能通过点击下面的“取消”按钮或返回键使其消失

              ③.点击外部使Popupwindow消失方法:

设置popupwindow的背景为空,设置点击外面消失的代码 reportVideoPopwindow.setOutsideTouchable(true); reportVideoPopwindow.setFocusable(true); reportVideoPopwindow.setBackgroundDrawable(new BitmapDrawable());


0 0