仿IOS弹窗/精美弹窗

先上效果图


//自定义style,让弹窗引用


布局:写弹窗样式

效果图:




布局代码:

画个Shape,下面布局代码会用到,给个圆角颜色:



xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/shape_ui_alert_view" android:orientation="vertical" >

 

 

 

 android:layout_height="0.5dp"

 android:background="@color/line_gray" />

 

android:layout_width="match_parent"

 android:layout_height="40dp"

 android:orientation="horizontal" >

 

 android:id="@+id/tvBtnLeft"

android:layout_width="match_parent"

 android:layout_height="match_parent"

 android:layout_weight="1"

 android:gravity="center" android:text="取消" android:textSize="14sp" />

 

 android:layout_width="0.3dp"

 android:layout_height="match_parent"

 android:background="#E0E0E0" />

android:id="@+id/tvBtnRight"

android:layout_width="match_parent"

 android:layout_height="match_parent"

 android:layout_weight="1"

android:gravity="center" android:text="确定" android:textSize="14sp" />

 



//自定义类继承Dialog

public class UIAlertViewextends Dialog {

private Contextcontext;

private Stringtitle;

private Stringmessage;

private StringbuttonLeftText;

private StringbuttonRightText;

private ClickListenerInterfaceclickListenerInterface;

public UIAlertView(Context context, String title, String message,

String buttonLeftText, String buttonRightText) {


super(context, R.style.UIAlertViewStyle);

this.context = context;

this.title = title;

this.message = message;

this.buttonLeftText = buttonLeftText;

this.buttonRightText = buttonRightText;

}

public interface ClickListenerInterface {

public void doLeft();

public void doRight();

}

@Override

    protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

inite();

}

public void inite() {

LayoutInflater inflater = LayoutInflater.from(context);


//这里是加载布局,上面已经也好了

View view = inflater.inflate(R.layout.ui_alert_view,null);

setContentView(view);


//获取布局中的ID

TextView tvMessage = (TextView) view.findViewById(R.id.tvMessage);

TextView tvLeft = (TextView) view.findViewById(R.id.tvBtnLeft);

TextView tvRight = (TextView) view.findViewById(R.id.tvBtnRight);

TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);

if ("".equals(title)) {

tvTitle.setVisibility(View.GONE);

}else {

tvTitle.setText(title);

}

tvMessage.setText(message);

tvLeft.setText(buttonLeftText);

tvRight.setText(buttonRightText);

tvLeft.setOnClickListener(new clickListener());

tvRight.setOnClickListener(new clickListener());

Window dialogWindow = getWindow();

WindowManager.LayoutParams lp = dialogWindow.getAttributes();

DisplayMetrics d =context.getResources().getDisplayMetrics();

lp.width = (int) (d.widthPixels *0.8);

dialogWindow.setAttributes(lp);

}

public void setClicklistener(ClickListenerInterface clickListenerInterface) {

this.clickListenerInterface = clickListenerInterface;

}

private class clickListenerimplements View.OnClickListener {

@Override

        public void onClick(View v) {

int id = v.getId();

switch (id) {

//左边的点击事件

case R.id.tvBtnLeft:

clickListenerInterface.doLeft();

break;



//右边的点击事件

case R.id.tvBtnRight:

clickListenerInterface.doRight();

break;

default:

break;

}

}

}

;

}




这里是具体使用

final UIAlertView delDialog =new UIAlertView(mContext,"温馨提示","确认删除该商品吗?",

"取消","确定");

delDialog.show();

delDialog.setClicklistener(new UIAlertView.ClickListenerInterface() {

@Override

public void doLeft() {

//左边按钮的点击事件 //.dismiss()消失弹窗

delDialog.dismiss();

}

//有点按钮的点击事件

@Override

                              public void doRight() {

delDialog.dismiss();

}

}

);


结束!

你可能感兴趣的:(仿IOS弹窗/精美弹窗)