原文网址:https://blog.csdn.net/u010694658/article/details/53022294

由于开发中经常使用弹框,然而系统自带的弹框太局限,也不太美观,经常不能满足开发需求,所以就只能自定义布局。其实自定义布局很简单,没不要写出来,但是如果不写一遍的,后面遇到的话就感觉又会忘记,所以在次记一小笔,仅记一个最简单的例子,可以举一反三。 

直接上代码

public class MainActivity extends Activity implements OnClickListener {

    private TextView text1, text2;
private Context mContext; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mContext = this;
initView(); } private void initView() {
text1 = (TextView) findViewById(R.id.text1);
text2 = (TextView) findViewById(R.id.text2); text1.setOnClickListener(this);
text2.setOnClickListener(this);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.text1:
dialogShow1();
break;
case R.id.text2:
dialogShow2();
break; default:
break;
}
} private void dialogShow1() {
AlertDialog.Builder builder = new Builder(mContext);
builder.setTitle("温馨提示");
builder.setIcon(R.drawable.ic_launcher);
builder.setMessage("原理是基本");
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(mContext, "no", 1).show();
}
});
builder.setPositiveButton("立即更新",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(mContext, "ok", 1).show();
}
});
Dialog dialog = builder.create();
dialog.show();
} /**
* 自定义布局
* setView()只会覆盖AlertDialog的Title与Button之间的那部分,而setContentView()则会覆盖全部,
* setContentView()必须放在show()的后面
*/
private void dialogShow2() {
AlertDialog.Builder builder = new Builder(mContext);
LayoutInflater inflater = LayoutInflater.from(mContext);
View v = inflater.inflate(R.layout.update_manage_dialog, null);
TextView content = (TextView) v.findViewById(R.id.dialog_content);
Button btn_sure = (Button) v.findViewById(R.id.dialog_btn_sure);
Button btn_cancel = (Button) v.findViewById(R.id.dialog_btn_cancel);
//builer.setView(v);//这里如果使用builer.setView(v),自定义布局只会覆盖title和button之间的那部分
final Dialog dialog = builder.create();
dialog.show();
dialog.getWindow().setContentView(v);//自定义布局应该在这里添加,要在dialog.show()的后面
//dialog.getWindow().setGravity(Gravity.CENTER);//可以设置显示的位置
btn_sure.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
dialog.dismiss();
Toast.makeText(mContext, "ok", 1).show();
}
}); btn_cancel.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
dialog.dismiss();
Toast.makeText(mContext, "no", 1).show();
}
});
} }

activity_main的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="100dp"
android:orientation="vertical" > <TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:text="弹出dialog"
android:textSize="@dimen/activity_horizontal_margin" /> <TextView
android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:text="弹出自定义布局dialog"
android:textSize="@dimen/activity_horizontal_margin" /> </LinearLayout>

update_manage_dialog布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FFFFFF" > <RelativeLayout
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_centerInParent="true"
android:background="@drawable/update_bg" > <TextView
android:id="@+id/dialog_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="温馨提示"
android:textSize="18sp" /> <TextView
android:id="@+id/dialog_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/dialog_title"
android:layout_marginTop="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:text="原理是基本\n实践出真知"
android:textSize="14sp" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" > <Button
android:id="@+id/dialog_btn_cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:text="取消"
android:textColor="#AAAAAA"
android:textSize="14sp" /> <Button
android:id="@+id/dialog_btn_sure"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:text="立即更新"
android:textSize="14sp" />
</LinearLayout>
</RelativeLayout> </RelativeLayout>

update_bg放在drawable里面,代码如下

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" > <!-- android:radius 弧形的半径 -->
<corners android:radius="30dp" /> <!-- 填充的颜色 -->
<solid android:color="@android:color/white" /> </shape>

【转】Android AlertDialog自定义布局的更多相关文章

  1. Android 创建自定义布局

    我们所有的控件都是继承至View类的,而所有的布局都是继承至ViewGroup的,所以我们也可以继承某个view类来实现我们自己的布局或者控件. 引入布局 我们新建一个title.xml的layout ...

  2. 获取 AlertDialog自定义的布局 的控件

    AlertDialog自定义的布局 效果图: 创建dialog方法的代码如下: 1 LayoutInflater inflater = getLayoutInflater(); 2 View layo ...

  3. Android之自定义AlertDialog和PopupWindow实现(仿微信Dialog)

    我们知道,在很多时候,我们都不用Android内置的一些控件,而是自己自定义一些自己想要的控件,这样显得界面更美观. 今天主要是讲自定义AlertDialog和popupWindow的使用,在很多需求 ...

  4. Android之自定义AlertDialog无法监听控件

    参考:http://www.cnblogs.com/511mr/archive/2011/10/21/2220253.html 要做一个自定义的弹出框,以前都是用一个Activity来实现,总觉得不是 ...

  5. Android:创建可穿戴应用 - 自定义布局

    创建自定义布局(Creating Custom Layouts) 本文将介绍如何创建自定义通知以及使用可穿戴UI库来创建自定义布局你同时还需要了解可穿戴设计准则(Wear Design Princip ...

  6. 【Android基础】listview控件的使用(4)-----自定义布局的listview的使用

    前面我介绍了listview控件的不同用法,但是这些用法在实际的开发项目中是不足以满足需求的,因为前面的几种用法只能简单的显示文本信息,而且布局都比较单一,很难做出复杂的结果,在实际的开发项目中,90 ...

  7. Android开发学习之路--UI之自定义布局和控件

    新的一年已经开始了,今天已经是初二了,两天没有学习了,还是要来继续学习下.一般手机的title都是actionbar,就像iphone一样可以后退,可以编辑.这里自定义布局就来实现下这个功能,首先准备 ...

  8. Android创建自定义的布局和控件

    Android的自带布局有framelayout.linerlayout.relativelayout,外加两个百分比布局,但是这些无法灵活的满足我们的需要,所以我们要自己自定义并引入自己的布局.首先 ...

  9. android 开发 使用自定义布局实现标题栏复用(标题栏内容自定义:使用代码实现和xml布局自定义属性2种办法实现)

    在个人学习的情况下可能很少使用自定义布局去实现大量复用的情况下,但是在一个开发工作的环境下就会使用到大量复用的自定义控件. 实现思维: 1.写一个xml的布局,用于标题栏的样式,并且添加在标题栏中你想 ...

随机推荐

  1. 关于jvm钩子 Runtime.getRuntime().addShutdownHook

    转自: http://www.cnblogs.com/nexiyi/p/java_add_ShutdownHook.html 在线上Java程序中经常遇到进程程挂掉,一些状态没有正确的保存下来,这时候 ...

  2. request 的下载文件

    前言:Content-Type类型为octets/stream,这种一般是文件类型了,比如有时候需要导出excel数据,下载excel这种场景如何用python来实现呢? 1.点击导出按钮 2.代码实 ...

  3. L314 单音节词读音规则(二)-元音字母发音规则

    1 单个元音发音尽量拖音一下(2S),发音会饱满一些. 2开音节: 辅音(辅组)(没有)+元音+辅音+e 的单词其中:元音发字母本身音,辅音字母不为r , 字母e不发音. 相对开音节:第一个元音都发字 ...

  4. day 21 模块 和 包

    一.模块-----(python代码的文件) 一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 为什么需要模块? 代码的可读性差,且重复的代码多,写代码困难大 ...

  5. Beta阶段复审

    名字 优点 缺点 排名 都行可以随它去 1.界面美观,比较的友好: 2.新增记账的邮箱提醒功能. 1.程序有什么具体的bug和或缺陷?(1)没有账单删除的功能:(2)预算没有什么必要.2.项目的目标实 ...

  6. finfo_file

      本文实例讲述了PHP使用finfo_file()函数检测上传图片类型的实现方法.分享给大家供大家参考,具体如下: 在输入输出中,文件的交互必不可少,比如文件的上传什么的.这里我们来解决一个小问题, ...

  7. Microsoft Project 常用快捷键

    任务升级 : ALT  +  SHIFT + 向左键 任务降级: ALT  +  SHIFT + 向右键 滚动到表头(第一个任务):Ctrl + HOME 滚动到表尾(最后一个任务):Ctrl + E ...

  8. 【Python】爬虫-1

    #练习1:获取搜狐网页上所有的URL并且把与篮球有关的内容筛选出来 #算法: #.获取搜狐网站所有内容 #.判断哪些是链接,获取URL格式有效的链接 #.获取每个有效URL网页的内容 #.判断内容是否 ...

  9. ES6 声明变量的6种方法

    ES5 只有两种声明变量的方法:var命令和function命令. ES6除了添加let和const命令,后面章节还会提到,另外两种声明变量的方法:import命令和class命令.所以,ES6 一共 ...

  10. Python 使用sqlalchemy操作MYSQL

    1. 安装sqlalchemy库 SQL操作引擎可能需要pymysql,故要安装如下两个程序 pip install sqlalchemy pip instal pymysql