Android 自定义AlertDialog,调用方法与系统一致

由于android原生的AlertDialog都一致,有时为了和你的项目的Dialog保持一致,你最先想到的就是有没有AlertDialog相关的style,但据我的查找,官方没有提供明确的文档来修改其样式,所以我们想到的是自己自定一个AlertDialog

如图,当然风格可以自己修改

所先把你想要的布局custom_dialog_view

<?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:orientation="vertical"
    android:gravity="center" >


    <!-- 顶部椭园边缘 -->


     <ImageView
        android:layout_width="300dp"
        android:layout_height="22dp"
        android:src="@drawable/app_listbk_d" >
    </ImageView>
    <!-- 中间白色背景,两个TextView,标题和内容,留一个LinearLayout,在代码中根据调用动态加上按钮 -->


    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:background="@drawable/app_listbk_d"
        android:orientation="vertical" >


        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textColor="#000000"
            android:textSize="35dp" />


        <TextView
            android:id="@+id/message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="20dp"
            android:textColor="#000000"
            android:textSize="25dp" />
        <!-- 在LinearLayout中加按钮 -->


        <LinearLayout
            android:id="@+id/buttonLayout"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:orientation="horizontal" >
        </LinearLayout>
    </LinearLayout>
    <!-- 底部椭园边缘 -->


     <ImageView
        android:layout_width="300dp"
        android:layout_height="22dp"
        android:layout_marginTop="-2dp"
        android:src="@drawable/app_listbk_d" >
    </ImageView>


</LinearLayout>


然后借助原生的AlertDialog来重写一下这个控制来达到我们的需求



package com.example.testapi.widget;


import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;


import com.example.testapi.R;


public class AlertDialog {
    Context context;
    android.app.AlertDialog ad;
    TextView titleView;
    TextView messageView;
    LinearLayout buttonLayout;


    public AlertDialog(Context context) {
        this.context = context;
        ad = new android.app.AlertDialog.Builder(context).create();
        ad.show();
        // Replace the source alert dialog.
        Window window = ad.getWindow();
        window.setContentView(R.layout.custom_dialog_view);
        titleView = (TextView) window.findViewById(R.id.title);
        messageView = (TextView) window.findViewById(R.id.message);
        buttonLayout = (LinearLayout) window.findViewById(R.id.buttonLayout);
    }


    public void setTitle(int resId)
    {
        titleView.setText(resId);
    }


    public void setTitle(String title) {
        titleView.setText(title);
    }


    public void setMessage(int resId) {
        messageView.setText(resId);
    }


    public void setMessage(String message)
    {
        messageView.setText(message);
    }


    /**
     * Button style
     * 
     * @param text
     * @param listener
     */
    public void setPositiveButton(String text, final View.OnClickListener listener)
    {
        Button button = new Button(context);
        LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        button.setLayoutParams(params);
        button.setBackgroundResource(R.drawable.app_listbk);
        button.setText(text);
        button.setTextColor(Color.WHITE);
        button.setTextSize(20);
        button.setOnClickListener(listener);
        buttonLayout.addView(button);
    }


    /**
     *  Button style
     * 
     * @param text
     * @param listener
     */
    public void setNegativeButton(String text, final View.OnClickListener listener)
    {
        Button button = new Button(context);
        LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        button.setLayoutParams(params);
        button.setBackgroundResource(R.drawable.app_listbk);
        button.setText(text);
        button.setTextColor(Color.WHITE);
        button.setTextSize(20);
        button.setOnClickListener(listener);
        if (buttonLayout.getChildCount() > 0)
        {
            params.setMargins(20, 0, 0, 0);
            button.setLayoutParams(params);
            buttonLayout.addView(button, 1);
        } else {
            button.setLayoutParams(params);
            buttonLayout.addView(button);
        }


    }


    /**
     * dismiss dialog
     */
    public void dismiss() {
        ad.dismiss();
    }


}

这样我们在自己的项目中就可以像使用原生的AlertDialog一样正常使用了

final AlertDialog ad = new AlertDialog(DialogStyleActivity.this);
    ad.setTitle("标题");
    ad.setMessage("dkdkkdkdk顺水有晨在于 在在在在在碍御用有有有地区专业分工有地介是的和无本之木工;地有胆有关有关 " +
    "耨溪水源源源码在紧俏商品22是否 2进行大跃进要核工业部");
    ad.setPositiveButton("确定", new OnClickListener() {


        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            ad.dismiss();
            Toast.makeText(DialogStyleActivity.this, "被点到确定", Toast.LENGTH_LONG).show();


        }
    });


    ad.setNegativeButton("取消", new OnClickListener() {


        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            ad.dismiss();
            Toast.makeText(DialogStyleActivity.this, "被点到取消", Toast.LENGTH_LONG).show();
        }
    });
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值