说明:从屏幕底部弹出PopupWindow,有弹出隐藏动画效果.背景设置透明度.
效果图如下:
1.MainActivity.java 显示popwindow,宽高跟屏幕大小一样,设置一个透明度背景
- public class MainActivity extends Activity {
- @Override
- public 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 parent = ((ViewGroup) this.findViewById(android.R.id.content)).getChildAt(0);
- 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);
- 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>
推荐下自己创建的android QQ群:202928390 欢迎大家的加入