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

因为android原生的AlertDialog都一致,有时为了和你的项目的Dialog保持一致,你最早想到的就是有没有AlertDialog相关的style,但据个人查找,官方没有提供明确的文档来修改其样式,因此咱们想到的是本身自定一个AlertDialogandroid

如图,固然风格能够本身修改api

所先把你想要的布局custom_dialog_viewapp

<?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>ide


而后借助原生的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同样正常使用了ui

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();
        }
    });
    }

this

相关文章
相关标签/搜索