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

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>

你可能感兴趣的:(android,自定义对话框)