本文主要是介绍自定义AlertDialog(圆角+退出动画+自定义布局),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
图片省略了不上传了。。。
首先看activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent" ><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="点击我,弹出AlertDialog" /></RelativeLayout>
MainActivity.java
package com.godinsec.alertdialog;import com.godinsec.alertdialog.ShowDeleDialog.onBtnClickListener;import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {new ShowDeleDialog(MainActivity.this, new onBtnClickListener(){@Overridepublic void onSure() {Toast.makeText(MainActivity.this, "进行确认操作", 0).show();}@Overridepublic void onExit() {Toast.makeText(MainActivity.this, "进行取消操作", 0).show();}}, "确定", "取消", "提示", "AlertDialog的内容").show();}});}}
接下来看自定义alertDialog
dele_allcontacts_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent" ><RelativeLayoutandroid:id="@+id/re_tip_dialog"android:layout_width="300dp"android:layout_height="120dp"android:layout_centerInParent="true"android:background="@drawable/rounded_search_text"><TextViewandroid:id="@+id/appTitle"android:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_marginLeft="10dp"android:layout_marginTop="10dp"android:text="标题"/><TextViewandroid:id="@+id/tips"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/appTitle"android:layout_alignLeft="@id/appTitle"android:text="AlertDialog的内容"android:layout_marginTop="20dp"/><TextViewandroid:id="@+id/sure"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:text="确认"android:layout_below="@id/tips"android:layout_marginTop="20dp" /><TextViewandroid:id="@+id/cancel"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignBottom="@id/sure"android:text="取消"android:layout_marginRight="30dp"android:layout_toLeftOf="@id/sure" /></RelativeLayout></RelativeLayout>
rounded_search_text.xml(圆角)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle" ><solid android:color="#FF7F24" /><cornersandroid:bottomLeftRadius="4dp"android:bottomRightRadius="4dp"android:topLeftRadius="4dp"android:topRightRadius="4dp" /></shape>
ShowDeleDialog.java
package com.godinsec.alertdialog;import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.ScaleAnimation;
import android.widget.RelativeLayout;
import android.widget.TextView;public class ShowDeleDialog extends Dialog implementsandroid.view.View.OnClickListener {private onBtnClickListener onBtnClickListener;private Context context;private String sure;private String cancle;private String appTitle;private String tips;private TextView tv_dele_sure;private TextView tv_dele_cancle;private TextView tv_appTitle;private TextView tv_tips;private RelativeLayout re_tip_dialog;public ShowDeleDialog(Context context,onBtnClickListener onBtnClickListener, String sure, String cancle,String appTitle, String tips) {super(context);this.onBtnClickListener = onBtnClickListener;this.context = context;this.appTitle = appTitle;this.tips = tips;this.sure = sure;this.cancle = cancle;}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 去除默认的头部标题requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.dele_allcontacts_dialog);tv_dele_sure = (TextView) findViewById(R.id.sure);tv_dele_cancle = (TextView) findViewById(R.id.cancel);tv_appTitle = (TextView) findViewById(R.id.appTitle);tv_tips = (TextView) findViewById(R.id.tips);re_tip_dialog = (RelativeLayout) findViewById(R.id.re_tip_dialog);tv_dele_cancle.setOnClickListener(this);tv_dele_sure.setOnClickListener(this);//进入动画是伪代码
ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 1.0f,
0.0f, 1.0f, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
scaleAnimation.setDuration(200);
re_tip_dialog.startAnimation(scaleAnimation);
Window window = this.getWindow();WindowManager.LayoutParams params = this.getWindow().getAttributes();// 去除四角黑色背景window.setBackgroundDrawable(new BitmapDrawable());// 设置周围的暗色系数params.dimAmount = 0.5f;window.setAttributes(params);// 为各个textview赋值tv_dele_sure.setText(sure);tv_dele_cancle.setText(cancle);tv_appTitle.setText(appTitle);tv_tips.setText(tips);}public interface onBtnClickListener {public void onSure();public void onExit();}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.cancel:onBtnClickListener.onExit();alertDialogExitAnim();break;case R.id.sure:onBtnClickListener.onSure();alertDialogExitAnim();break;default:break;}}private void alertDialogExitAnim() {ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.0f,1.0f, 0.0f, Animation.ABSOLUTE,re_tip_dialog.getWidth() / 2, Animation.ABSOLUTE,re_tip_dialog.getHeight() / 2);scaleAnimation.setDuration(1000);re_tip_dialog.startAnimation(scaleAnimation);scaleAnimation.setAnimationListener(new AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationRepeat(Animation animation) {}@Overridepublic void onAnimationEnd(Animation animation) {ShowDeleDialog.this.dismiss();}});}}
这篇关于自定义AlertDialog(圆角+退出动画+自定义布局)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!