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

相關文章
相關標籤/搜索