android dialog 自定义布局,自定义AlertDialog布局

这篇博客展示了如何在Android中使用LayoutInflater和AlertDialog创建一个自定义的对话框,包括设置消息内容、确定和取消按钮,并实现了点击事件。同时,还提供了像素单位转换的工具类DensityUtil。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先看效果图:

f7c8197ec0d44404bc4602ef1c552a2f.png

附上代码:

// 1. 布局文件转换为View对象

LayoutInflater inflater = LayoutInflater.from(this);

RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.alert_dialog_001, null);

// 2. 新建对话框对象

final Dialog dialog = new AlertDialog.Builder(MainActivity.this).create();

dialog.setCancelable(false);

dialog.show();

dialog.getWindow().setContentView(layout);

// 3. 消息内容

TextView dialog_msg = (TextView) layout.findViewById(R.id.dialog_msg);

dialog_msg.setText("你确定吗?");

// 4. 确定按钮

Button btnOK = (Button) layout.findViewById(R.id.dialog_ok);

btnOK.setOnClickListener(new OnClickListener()

{

@Override

public void onClick(View v)

{

Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_SHORT).show();

dialog.dismiss();

}

});

// 5. 取消按钮

Button btnCancel = (Button) layout.findViewById(R.id.dialog_cancel);

btnCancel.setOnClickListener(new OnClickListener()

{

@Override

public void onClick(View v)

{

Toast.makeText(getApplicationContext(), "cancel", Toast.LENGTH_SHORT).show();

dialog.dismiss();

}

});

// 6. 只显示一个按钮

btnCancel.setVisibility(View.GONE);

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(DensityUtil.dip2px(context, 120), DensityUtil.dip2px(context, 48));

btnOK.setLayoutParams(params);

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="#00000000" >

android:id="@+id/rl_dialog_content"

android:layout_width="match_parent"

android:layout_height="200dp"

android:layout_marginLeft="40dp"

android:layout_marginRight="40dp"

android:background="@drawable/shape_top_corner_no_bottom_line"

android:padding="8dp" >

android:id="@+id/dialog_title"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="20dp"

android:gravity="center"

android:singleLine="true"

android:text="提示"

android:textSize="18sp" />

android:id="@+id/dialog_msg"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:layout_marginLeft="20dp"

android:layout_marginRight="20dp"

android:gravity="center"

android:maxLines="3"

android:text="你父母知道吗?"

android:textSize="18sp" />

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:gravity="center"

android:orientation="horizontal" >

android:id="@+id/dialog_ok"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:background="@drawable/dialog_btn_ok"

android:text="确定"

android:textColor="#ffffff" />

android:id="@+id/dialog_cancel"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="5dp"

android:layout_weight="1"

android:background="@drawable/dialog_btn_cancle"

android:text="取消" />

像素转换工具类:

import android.content.Context;

public class DensityUtil

{

/**

* 根据手机的分辨率从 dp 的单位 转成为 px(像素)

*/

public static int dip2px(Context context, float dpValue)

{

final float scale = context.getResources().getDisplayMetrics().density;

return (int) (dpValue * scale + 0.5f);

}

/**

* 根据手机的分辨率从 px(像素) 的单位 转成为 dp

*/

public static int px2dip(Context context, float pxValue)

{

final float scale = context.getResources().getDisplayMetrics().density;

return (int) (pxValue / scale + 0.5f);

}

}

背景样式:

还有两个按钮图片就不传了,大家自定义。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值