由于Android系统中的AlterDialog界面不是很好看,而且整体颜色为灰黑,有可能和你做的项目不太搭调,在使用AlterDialog的时候,许多人都想使用自定义的的布局,这样灵活性高,布局也会比较好看,但是要给自定义布局中的控件添加监听事件,好像就不是那么一回事了,今天我就为大家写一个小小的demo,有不足之处还希望大家能够积极指出,在此感激不尽。
res/layout/activity_main.xml
- <LinearLayout 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"
- android:orientation="vertical" >
- <Button
- android:layout_gravity="center"
- android:id="@+id/btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="显示弹出框"
- android:background="@drawable/button_border_radius"/>
- </LinearLayout>
上面的背景是自定义的一个带圆角边框的按钮背景
res/drwable/button_border_radius.xml
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android" >
- <corners android:radius="30.0px" />
- <solid android:color="#fc3d84" />
- </shape>
- </pre><pre name="code" class="html">在定义一个弹出框的布局
- res/layout/alter_dialog_layout.xml
- </pre><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:paddingTop="20dp"
- android:paddingBottom="10dp"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_marginTop="10dp"
- android:id="@+id/alter_dialog_message"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:padding="5dp"
- android:text="亲爱哒,这个相册的照片很漂亮哒,是不是真的要删除呢?"
- android:textSize="16sp" />
- <LinearLayout
- android:layout_marginTop="20dp"
- android:id="@+id/alter_dialog_button"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:orientation="horizontal" >
- <TextView
- android:id="@+id/alter_dialog_button_ensure"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/button_border_radius"
- android:clickable="true"
- android:paddingBottom="3dp"
- android:paddingLeft="20dp"
- android:paddingRight="20dp"
- android:paddingTop="3dp"
- android:text="狠心删除"
- android:textColor="@android:color/white" />
- <TextView
- android:id="@+id/alter_dialog_button_cancel"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="50dp"
- android:background="@drawable/button_border_radius"
- android:clickable="true"
- android:paddingBottom="3dp"
- android:paddingLeft="20dp"
- android:paddingRight="20dp"
- android:paddingTop="3dp"
- android:text="再看看"
- android:textColor="@android:color/white" />
- </LinearLayout>
- </LinearLayout>
基本的布局已经定义好了,现在关键的activity来了
src/MainActivity
- package com.example.test;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- public class MainActivity extends Activity {
- private AlertDialog dialog;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Button btn = (Button) findViewById(R.id.btn);
- btn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- showDialog();
- }
- });
- }
- /**
- * 显示dialog
- */
- private void showDialog(){
- LayoutInflater inflater = LayoutInflater.from(this);
- View view = inflater.inflate(R.layout.alter_dialog_layout, null);
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setView(view);
- TextView tv = (TextView) view.findViewById(R.id.alter_dialog_message);
- tv.setText(" 亲爱哒,你确定要删除这个相册吗?");
- TextView ensure = (TextView) view.findViewById(R.id.alter_dialog_button_ensure);
- TextView cancel = (TextView) view.findViewById(R.id.alter_dialog_button_cancel);
- ensure.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- //这里执行相应的删除代码
- dialog.dismiss();
- }
- });
- cancel.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- dialog.dismiss();
- }
- });
- dialog = builder.create();
- dialog.setView(view, 0, 0, 0, 0);
- dialog.show();
- }
- }