码迷,mamicode.com
首页 > 移动开发 > 详细

Android 自定义PopupWindow类似iOS的ActionSheet

时间:2015-03-03 16:47:42      阅读:771      评论:0      收藏:0      [点我收藏+]

标签:android   自定义popupwindow   ios actionsheet   弹出菜单   

效果图如下:

技术分享


核心代码:

package com.zms.actionsheet;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface.OnCancelListener;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ActionSheet {

    public interface OnActionSheetSelected {
        void onClick(int whichButton);
    }

    private ActionSheet() {
    }

    public static Dialog showSheet(Context context, final OnActionSheetSelected actionSheetSelected,
                                   OnCancelListener cancelListener) {
        final Dialog dlg = new Dialog(context, R.style.ActionSheet);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.actionsheet, null);
        final int cFullFillWidth = 10000;
        layout.setMinimumWidth(cFullFillWidth);

        TextView mContent = (TextView) layout.findViewById(R.id.content);
        TextView mCancel = (TextView) layout.findViewById(R.id.cancel);

        mContent.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                actionSheetSelected.onClick(0);
                dlg.dismiss();
            }
        });

        mCancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                actionSheetSelected.onClick(1);
                dlg.dismiss();
            }
        });

        Window w = dlg.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.x = 0;
        final int cMakeBottom = -1000;
        lp.y = cMakeBottom;
        lp.gravity = Gravity.BOTTOM;
        dlg.onWindowAttributesChanged(lp);
        dlg.setCanceledOnTouchOutside(false);
        if (cancelListener != null)
            dlg.setOnCancelListener(cancelListener);

        dlg.setContentView(layout);
        dlg.show();
        return dlg;
    }
}



转载请注明出处:周木水的CSDN博客 http://blog.csdn.net/zhoumushui 
我的GitHub:周木水的GitHub https://github.com/zhoumushui

Android 自定义PopupWindow类似iOS的ActionSheet

标签:android   自定义popupwindow   ios actionsheet   弹出菜单   

原文地址:http://blog.csdn.net/zhoumushui/article/details/44038499

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!