Android 仿 Iphone 弹出提示框

  1. 自定义 布局 代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@drawable/iphone_dialog"
              android:gravity="center"
              android:orientation="vertical"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1.5"
        android:orientation="vertical"
        android:layout_gravity="center_vertical"
        >
        <TextView
            android:id="@+id/dialog_content"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:textColor="@color/black"
            android:text="content"
            />
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/s_1"
        android:background="@color/gray"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1.0"
        android:orientation="horizontal"
        android:gravity="center"
        >

        <Button
            android:id="@+id/dialg_cancel"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="取消"
            android:background="@null"
            android:textColor="@color/font_blue"
            />

        <View
            android:layout_width="@dimen/s_1"
            android:layout_height="match_parent"
            android:background="@color/gray"
            />

        <Button
            android:id="@+id/dialog_confirm"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="确定"
            android:background="@null"

            android:textColor="@color/font_blue"
            />
    </LinearLayout>


</LinearLayout>

2. 自定义 Dialog IphoneDialog

public class IPhoneDialog extends Dialog {
   public IPhoneDialog(Context context) {
      super(context);
   }

   public IPhoneDialog(Context context, int themeResId) {
      super(context, themeResId);
   }

   protected IPhoneDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
      super(context, cancelable, cancelListener);
   }


   public static class Builder{
      private Context context;
      private String message;
      private String positiveButtonText;
      private String negativeButtonText;
      private View contentView;
      private DialogInterface.OnClickListener positiveButtonClickListener;
      private DialogInterface.OnClickListener negativeButtonClickListener;

      public Builder(Context context) {
         this.context = context;
      }

      public Builder setMessage(String message) {
         this.message = message;
         return this;
      }

      /**
       * Set the Dialog message from resource
       *
       * @param message
       * @return
       */
      public Builder setMessage(int message) {
         this.message = (String) context.getText(message);
         return this;
      }

      public Builder setContentView(View v) {
         this.contentView = v;
         return this;
      }

      /**
       * Set the positive button resource and it's listener
       *
       * @param positiveButtonText
       * @return
       */
      public Builder setPositiveButton(int positiveButtonText,
                                       DialogInterface.OnClickListener listener) {
         this.positiveButtonText = (String) context
               .getText(positiveButtonText);
         this.positiveButtonClickListener = listener;
         return this;
      }

      public Builder setPositiveButton(String positiveButtonText,
                                       DialogInterface.OnClickListener listener) {
         this.positiveButtonText = positiveButtonText;
         this.positiveButtonClickListener = listener;
         return this;
      }

      public Builder setNegativeButton(int negativeButtonText,
                                       DialogInterface.OnClickListener listener) {
         this.negativeButtonText = (String) context
               .getText(negativeButtonText);
         this.negativeButtonClickListener = listener;
         return this;
      }

      public Builder setNegativeButton(String negativeButtonText,
                                       DialogInterface.OnClickListener listener) {
         this.negativeButtonText = negativeButtonText;
         this.negativeButtonClickListener = listener;
         return this;
      }
      public IPhoneDialog create(){
         LayoutInflater inflater = (LayoutInflater) context
               .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         IPhoneDialog dialog= new IPhoneDialog(context,R.style.dialog);
         View view = inflater.inflate(R.layout.iphone_dialog,null);
         int height = TDevice.getDisplayMetrics().heightPixels;
         int width = TDevice.getDisplayMetrics().widthPixels;
         dialog.addContentView(view, new ViewGroup.LayoutParams(width*2/3,height/6));
         ((TextView)view.findViewById(R.id.dialog_content)).setText(message);
         if (positiveButtonText !=null){
            ((Button)view.findViewById(R.id.dialog_confirm)).setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                  positiveButtonClickListener.onClick(dialog,
                        DialogInterface.BUTTON_POSITIVE);
               }
            });
         }else{
            view.findViewById(R.id.dialog_confirm).setVisibility(
                  View.GONE);
         }

         if (negativeButtonText !=null){
            ((Button)view.findViewById(R.id.dialg_cancel)).setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                  negativeButtonClickListener.onClick(dialog,
                        DialogInterface.BUTTON_POSITIVE);
               }
            });
         }else{
            view.findViewById(R.id.dialg_cancel).setVisibility(
                  View.GONE);
         }
         // 点击外部 空白处,不消失
         dialog.setCanceledOnTouchOutside(false);
         return dialog;
      }
   }

}

3. 涉及到的 style 样式添加:

<style name="dialog" parent="@android:style/Theme.Dialog">
    <!-- 可 去除  使用shape 后  四个边角 出现黑色 显示-->
    <item name="android:windowBackground">@android:color/transparent</item>

    <item name="android:windowFrame">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

4. 圆角显示样式:

 

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="@dimen/s_10"/>
    <gradient android:startColor="@color/white"
              android:endColor="@color/white"
        />
    <stroke android:width="@dimen/s_1" android:color="@color/white"/>

</shape>


你可能感兴趣的:(Android 仿 Iphone 弹出提示框)