2019独角兽企业重金招聘Python工程师标准>>>

先看效果图:

附上代码:

// 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()
{@Overridepublic 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()
{@Overridepublic 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);
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#00000000" ><RelativeLayoutandroid: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" ><TextViewandroid: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" /><TextViewandroid: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" /><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:gravity="center"android:orientation="horizontal" ><Buttonandroid: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" /><Buttonandroid: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="取消" /></LinearLayout></RelativeLayout></RelativeLayout>

像素转换工具类:

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

背景样式:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item><shape><stroke android:width="2dp" android:color="#CCCCCC"/><solid android:color="#FFFFFF" /><corners android:radius="6dp" /></shape></item></selector>

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

转载于:https://my.oschina.net/u/1036767/blog/266317

自定义AlertDialog布局相关推荐

  1. android activity传值到dialog,android 自定义AlertDialog 与Activity相互传递数据

    **主要实现功能:** 1.从Activity的TextView中获取字符串设置到AlertDialog的TextView和EditText中 2.将AlertDialog的EditText中的值设置 ...

  2. Android自定义AlertDialog

    常见的一种方法: [html] view plaincopyprint? AlertDialog.Builder builder; AlertDialog alertDialog; LayoutInf ...

  3. 使用这个,自定义AlertDialog在你手里都不是问题

    2019独角兽企业重金招聘Python工程师标准>>> 最近一直在搞自定义的AlertDialog,先贴一下图. 这几种都是自定义的AlertDialog,还支持从Service弹出 ...

  4. 自定义AlertDialog.Builder对话框

    在工作中,原生态的AlertDialog已经远远不能满足我们工作的需求,这时候,就需要使用自定义的AlertDialog.Builder: 在自定义之前,我们先了解下, **原生态的AlertDial ...

  5. Android自定义AlertDialog的控件获取操作

    Android自定义AlertDialog的控件获取操作 在自定义的AlertDialog布局虽然可以显示,但是试过很多方法都不能获得其中的控件进行操作,找了很多方法最后这种方法可以. dialog的 ...

  6. 创建自定义AlertDialog

    没什么特别的地方,只是自定义弹出一个简单的对话框. 效果图: 首先定义一个布局文件 custom_dialog.xml: <?xml version="1.0" encodi ...

  7. R语言可视化、编写自定义函数可视化水平排序条形图(horizontal bar plot)、自定义图像布局模仿经济学人杂志可视化效果、右侧添加标签数值图像方框、自定义背景色、水平条形图中间线条等

    R语言可视化.编写自定义函数可视化水平排序条形图(horizontal bar plot).自定义图像布局模仿经济学人杂志可视化效果.右侧添加标签数值图像方框.自定义背景色.水平条形图中间线条.网格线 ...

  8. android 前台服务自定义布局不显示_Android自定义LinearLayout布局显示不完整的解决方法...

    发现问题 原需求,在一个伸缩列表中,自定义LinearLayout继承LinearLayout动态添加布局. 然而实现的时候:一共遍历了30条数据,却只显示了一条 断点查看代码:遍历addView() ...

  9. 自定义ActionBar、Toolbar布局(解决自定义ActionBar布局左边始终有一点边距的问题)

    转载地址:http://blog.csdn.net/zxt0601/article/details/50463319 先列出 自定义ActionBar布局左边始终有一点边距的问题的解决办法: 左边的那 ...

最新文章

  1. matlab 几何概率
  2. mysql忘记密码如何修改
  3. 驱动编程中的头文件与内核源码的关系
  4. Python中*args 和**kwargs的用法
  5. Codeforces344_C(数学思维)
  6. 《程序员的呐喊》读书笔记
  7. 使用console.table()调试javascript
  8. 大端字节序小端字节序(网络字节序主机字节序)
  9. 如何找到最快的DNS服务器!!
  10. 条码的应用在管理上具有便捷的特点
  11. Python命令行版本的员工管理系统
  12. 企业如何做好EDM-企业做EDM的方案设计
  13. 将一个javaBean中非空的属性合并到另一个javaBean中
  14. 计算机保护插件无法安装,电脑无法安装ActiveX控件怎么办
  15. Vue 轻量级后台管理系统基础模板
  16. ugui 转轮_Unity3D的FingerGesture插件
  17. CWE-通用弱点枚举简介
  18. 20200720《Flask进阶》——蓝本
  19. toLower toUpper
  20. [UNR#5]诡异操作

热门文章

  1. DCMTK:测试图像像素模块功能
  2. VTK:可视化之BackgroundTexture
  3. OpenCV imgproc分割(segmentation)的实例(附完整代码)
  4. OpenCV钻石Diamond 标记的检测
  5. OpenCV重新映射Remapping
  6. C++happy number开心数的实现算法(附完整源码)
  7. QT的QMutableHashIterator类的使用
  8. QT的QDBusInterface类的使用
  9. html5 制作风车,[网页设计]html5 requestAnimationFrame制作动画:旋转风车
  10. 19、20_散点图、连接散点图、气泡图、不同颜色的散点图、直方图