Android之自定义对话框AlertDialog.Builder+getLayoutInflater().inflate

本文介绍如何在Android应用中通过AlertDialog.Builder结合getLayoutInflater().inflate方法自定义对话框。具体步骤包括在AccountDetailActivity.java中设置逻辑,利用activity_account_detail.xml作为整体布局,以及创建dialog_confirm.xml作为自定义对话框的布局文件。
activity文件:AccountDetailActivity.java
public class AccountDetailActivity extends BaseActivity{
    private View configLayout;
    configLayout = view.findViewById(R.id.configDetailLayout);
    configLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                final AlertDialog dialog = new AlertDialog.Builder(AccountDetailActivity.this).create();//创建一个AlertDialog对象 
                View view = getLayoutInflater().inflate(R.layout.dialog_confirm, null);//自定义布局
                TextView message  = (TextView)view.findViewById(R.id.message);
                message.setText("是否退出?");
                View ok  =  view.findViewById(R.id.ok);
                ok.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        app.logout();
                        app.setVersion(app.getVersion() + 1);

                        AccountDetailActivity.this.finish();
                    }}
                );
                View cancel = view.findViewById(R.id.cancel);
                cancel.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {                       
                        //nothing to do
                        dialog.dismiss();
                    }}
                );
                dialog.setView(view, 0, 0, 0, 0);//把自定义的布局设置到dialog中,注意,布局设置一定要在show之前。从第二个参数分别填充内容与边框之间左、上、右、下、的像素
                dialog.show();//一定要先show出来再设置dialog的参数,不然就不会改变dialog的大小了            
            }
        });
}
整体布局文件:activity_account_detail.xml
<RelativeLayout
        android:id="@+id/configDetailLayout"
        android:layout_width="match_parent"
        android:layout_height="50dip"
        android:background="@drawable/gray_selector"
        android:clickable="true"
        android:paddingLeft="20dip"
        android:paddingRight="20dip" >

        <TextView
            style="@style/text_s32_1c1c1c"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:text="退出登录" />
    </RelativeLayout>
自定义布局文件:dialog_confirm.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="200dip"
    android:layout_height="150dip"
    android:background="@color/white"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent" 
        android:layout_height="100dip">
        <TextView 
            android:id="@+id/message"
                    style="@style/text_s28_1c1c1c"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="是否要删除该记录?"/>
    </RelativeLayout>


    <RelativeLayout
    android:id="@+id/menu" 
    android:layout_width="match_parent" 
    android:layout_height="50dip">
    <View  
            android:layout_width="fill_parent"  
            android:layout_height="1px"
            android:layout_alignParentTop="true"
            android:background="@color/cdddddd"/>  

    <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_centerVertical="true">

        <RelativeLayout
            android:id="@+id/cancel" 
            android:layout_width="match_parent"
            android:layout_height="49dip"
            android:background="@drawable/white_selector"
            android:layout_weight="1" >
            <LinearLayout 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true">
                <TextView
                    android:id="@+id/cancel_text"  
                    style="@style/text_s28_1c1c1c"
                    android:layout_gravity="center_vertical"
                    android:text="取消"/>

            </LinearLayout>

        </RelativeLayout>

         <View  
            android:layout_width="1dip"  
            android:layout_height="25dip"
            android:layout_gravity="center"
            android:background="@color/cdddddd"/>

        <RelativeLayout
            android:id="@+id/ok" 
            android:layout_width="match_parent"
            android:layout_height="49dip"
            android:background="@drawable/white_selector"
            android:layout_weight="1" >
            <LinearLayout 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true">
                <TextView
                    android:id="@+id/ok_text" 
                    style="@style/text_s28_1c1c1c"
                    android:layout_gravity="center_vertical"
                    android:text="确定"/>                 
            </LinearLayout>

        </RelativeLayout>

    </LinearLayout>
    <View  
            android:layout_width="fill_parent"  
            android:layout_height="1dip"
            android:layout_alignParentBottom="true"
            android:background="@color/cdddddd"/>  

</RelativeLayout>

</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值