仿微信弹出框

仿微信弹出框_第1张图片

1: 

public class SettingWindow extends PopupWindow {

    private final Context context;
    private final OnItemCheckedListener listener;
    private final View rootView;

    private int width;

    String[] data;


    public SettingWindow(Context context, View rootView, String[] data, OnItemCheckedListener listener) {
        super(context);
        this.context = context;
        this.rootView = rootView;
        this.listener = listener;
        this.data = data;
        width = UIUtil.dip2px(200);
        initView();
    }

    private void initView() {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_setting_window, null);
        initUI(view);

        setContentView(view);
        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setAnimationStyle(R.style.BottomFade);
        //使其可以在窗口外点击取消
        setOutsideTouchable(true);
        setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x00000000);
        setBackgroundDrawable(dw);

    }

    private void initUI(View view) {
        final RadioGroup mGroup = view.findViewById(R.id.rg_setting);


        for (int i = 0; i < data.length; i++) {
            RadioButton button = new RadioButton(context);
            button.setButtonDrawable(null);
            button.setId(i);
            button.setText(data[i]);
            button.setTextSize(16);
            button.setTextColor(UIUtil.getColor(R.color.black));
            button.setGravity(Gravity.CENTER);
            button.setBackgroundResource(R.drawable.shape_custom_dialog);

            RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(width, UIUtil.dip2px(40));
            params.bottomMargin = UIUtil.dip2px(12);

            button.setLayoutParams(params);

            mGroup.addView(button);
        }


        mGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                dismiss();
                RadioButton button = group.findViewById(checkedId);
                button.setChecked(false); // 可以多次选中
                listener.onChecked(checkedId);
            }
        });

    }

    public interface OnItemCheckedListener {
        void onChecked(int checkedId);
    }


    public void show() {
        showAtLocation(rootView, Gravity.BOTTOM, 0, 0);
    }
}

2:




    

3:   R.style.BottomFade

  

 

你可能感兴趣的:(自定义控件)