欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

仿IOS样式对话框

程序员文章站 2024-03-21 12:10:04
...

效果图如下:

仿IOS样式对话框


实现上述功能所需步骤:

步骤一:自定义Dialog,并继承Dialog

public class DrinkWateDialog extends Dialog {

    private static int default_width = 160; // 默认宽度
    private static int default_height = 120;// 默认高度

    private boolean noBackPressed = false;

    public DrinkWateDialog(Context context, View layout, int style) {
        this(context, default_width, default_height, layout, style);
    }

    public DrinkWateDialog(Context context, int width, int height, View layout, int style) {
        super(context, style);
        // 加载布局
        setContentView(layout);
        // 设置Dialog参数
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);
    }

    /**
     * 设置返回键
     *
     * @param b
     */
    public void setBackPressed(boolean b) {
        noBackPressed = b;
    }

    @Override
    public void onBackPressed() {
        if (!noBackPressed) {
            super.onBackPressed();
        }
    }
}

步骤二:显示dialog,并为每个内容添加点击事件

 btn= (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog();
            }
        });


    }


    public  void  showDialog(){
        View view = View.inflate(this, R.layout.drinkwater_dialog, null);
        DrinkWateDialog kCornerDialog = new DrinkWateDialog(this, 0, 0, view, R.style.DrinkWateTextViewDialog);
        kCornerDialog.show();


    }

   public  void  tvOnClick(View view){
       switch (view.getId()){
           case  R.id.tv_1:
               break;

           case  R.id.tv_2:
               break;

           case  R.id.tv_3:
               break;

           case  R.id.tv_4:
               break;
       }
   }

好了,完成

源码下载地址:http://download.csdn.net/detail/k2514091675/9912648