自定义dialog,实现右上角显示一个控件按钮
2014-05-26 14:11
375 查看
原文地址 http://blog.csdn.net/BBLD_/article/details/27070531
这里是使用自定义dialog的布局实现,并去除原生dialog的标题。
以下是dialog布局的xml文件:
[html] view
plaincopy
<?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:layout_margin="50dp"
android:background="@android:color/transparent"
android:gravity="center" >
<LinearLayout
android:id="@+id/LL_this"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="9dp"
android:layout_marginRight="9dp"
android:layout_marginTop="9dp"
android:background="@drawable/rounded_background"
android:orientation="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="30dp"
android:shrinkColumns="1" >
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="课室: " />
<TextView
android:id="@+id/txt_pre_entry_dialog_classroom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="数据异常" />
</TableRow>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/gray" />
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="老师: " />
<TextView
android:id="@+id/txt_pre_entry_dialog_teacher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="数据异常" />
</TableRow>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/gray" />
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="课程: " />
<TextView
android:id="@+id/txt_pre_entry_dialog_course"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="数据异常" />
</TableRow>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/gray" />
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="班级: " />
<TextView
android:id="@+id/txt_pre_entry_dialog_classes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="数据异常" />
</TableRow>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="10dp"
android:background="@color/gray" />
</TableLayout>
</LinearLayout>
<ImageButton
android:id="@+id/dialog_pre_entry_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/cancel" />
</RelativeLayout>
要实现在右上角偏移突出显示一个关闭的Button,使用RelativeLayout方便点。同时第6、13、14、15行也起了作用,有什么作用效果大家动下手修改修改就知道了。
然后就要到代码里去设置dialog了,如下:
[java] view
plaincopy
private Dialog allMsg;
//Dialog的布局View
private View allMsgView;
// 通过LayoutInflater找到改布局
allMsgView = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.dialog_all_pre_entry_msg, null);
//创建Dialog
allMsg = new AlertDialog.Builder(this).create();
//设置点击外边缘不消失,2.x的应该是默认不消失的
allMsg.setCanceledOnTouchOutside(false);
//findView布局里的控件
imgBtn_dialog = (ImageButton) allMsgView.findViewById(R.id.dialog_pre_entry_close);
imgBtn_dialog.setOnClickListener(this);
txt_dialog_classroom = (TextView) allMsgView.findViewById(R.id.txt_pre_entry_dialog_classroom);
txt_dialog_course = (TextView) allMsgView.findViewById(R.id.txt_pre_entry_dialog_course);
txt_dialog_teacher = (TextView) allMsgView.findViewById(R.id.txt_pre_entry_dialog_teacher);
txt_dialog_classes = (TextView) allMsgView.findViewById(R.id.txt_pre_entry_dialog_classes);
然后载你需要弹出的地方调用如下:
[java] view
plaincopy
//两句的顺序不能调换
allMsg.show();
allMsg.getWindow().setContentView((RelativeLayout) allMsgView);
取消在关闭按钮的监听了关闭dialog的行了:
[java] view
plaincopy
/**
* 按钮监听
*/
@Override
public void onClick(View v)
{
switch (v.getId())
{
// dialog的图片取消button
case R.id.dialog_pre_entry_close:
allMsg.dismiss();
break;
default:
break;
}
}
布局效果图:
这里是使用自定义dialog的布局实现,并去除原生dialog的标题。
以下是dialog布局的xml文件:
[html] view
plaincopy
<?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:layout_margin="50dp"
android:background="@android:color/transparent"
android:gravity="center" >
<LinearLayout
android:id="@+id/LL_this"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="9dp"
android:layout_marginRight="9dp"
android:layout_marginTop="9dp"
android:background="@drawable/rounded_background"
android:orientation="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="30dp"
android:shrinkColumns="1" >
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="课室: " />
<TextView
android:id="@+id/txt_pre_entry_dialog_classroom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="数据异常" />
</TableRow>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/gray" />
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="老师: " />
<TextView
android:id="@+id/txt_pre_entry_dialog_teacher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="数据异常" />
</TableRow>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/gray" />
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="课程: " />
<TextView
android:id="@+id/txt_pre_entry_dialog_course"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="数据异常" />
</TableRow>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/gray" />
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="班级: " />
<TextView
android:id="@+id/txt_pre_entry_dialog_classes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="数据异常" />
</TableRow>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="10dp"
android:background="@color/gray" />
</TableLayout>
</LinearLayout>
<ImageButton
android:id="@+id/dialog_pre_entry_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/cancel" />
</RelativeLayout>
要实现在右上角偏移突出显示一个关闭的Button,使用RelativeLayout方便点。同时第6、13、14、15行也起了作用,有什么作用效果大家动下手修改修改就知道了。
然后就要到代码里去设置dialog了,如下:
[java] view
plaincopy
private Dialog allMsg;
//Dialog的布局View
private View allMsgView;
// 通过LayoutInflater找到改布局
allMsgView = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.dialog_all_pre_entry_msg, null);
//创建Dialog
allMsg = new AlertDialog.Builder(this).create();
//设置点击外边缘不消失,2.x的应该是默认不消失的
allMsg.setCanceledOnTouchOutside(false);
//findView布局里的控件
imgBtn_dialog = (ImageButton) allMsgView.findViewById(R.id.dialog_pre_entry_close);
imgBtn_dialog.setOnClickListener(this);
txt_dialog_classroom = (TextView) allMsgView.findViewById(R.id.txt_pre_entry_dialog_classroom);
txt_dialog_course = (TextView) allMsgView.findViewById(R.id.txt_pre_entry_dialog_course);
txt_dialog_teacher = (TextView) allMsgView.findViewById(R.id.txt_pre_entry_dialog_teacher);
txt_dialog_classes = (TextView) allMsgView.findViewById(R.id.txt_pre_entry_dialog_classes);
然后载你需要弹出的地方调用如下:
[java] view
plaincopy
//两句的顺序不能调换
allMsg.show();
allMsg.getWindow().setContentView((RelativeLayout) allMsgView);
取消在关闭按钮的监听了关闭dialog的行了:
[java] view
plaincopy
/**
* 按钮监听
*/
@Override
public void onClick(View v)
{
switch (v.getId())
{
// dialog的图片取消button
case R.id.dialog_pre_entry_close:
allMsg.dismiss();
break;
default:
break;
}
}
布局效果图:
相关文章推荐
- Android 自定义dialog,实现右上角显示一个控件按钮
- GGrid网页手写签批控件个人实现在页面上自定义显示的一个例子
- 在窗体上放置一个标签、一个按钮、一个fontDialog控件。标签内容改为“烟台大学”。实现功能:1)程序运行时,单击打开字体对话框按钮,可选择字体,并以所选字体作为标签字体。
- iOS cocos2d实现自定义button(按钮特效)控件效果源码
- VB编写一个能显示百分比的自定义进度条控件
- C#--第11周实验--任务3--设计一个窗体,窗体上有两个文本框,一个按钮,2个单选按钮--实现单击按钮后,根据单选按钮,将对应文本框中内容显示在标签。
- 实现一个Asp.net自定义Back控件
- 通过js选择按钮实现一个内容隐藏另一个内容显示
- 自定义Repeater数据控件的一个页面显示效果
- C#程序设计(二十六)----创建一个如下的窗体,并在窗体上放置一个标签、一个按钮、一个fontDialog控件
- F:在datagrid中 怎么实现 点击一行(多列)传一个id过去到第二页显示其具体内容,不用按钮、链接等?
- 编写Applet程序实现一个计算器,包括10个数字(0~9)按钮和4个运算符(加、减、乘、除)按钮,以及等号和清空两个辅助按钮,还有一个显示输入输出的文本框。试用GridLayout实现之
- 实现一个模拟CMD.exe命令编辑模式执行与显示的Delphi控件
- Delphi组件开发教程指南(6)实现一个模拟动画显示控件
- C#第十周任务之最后一项之创建一个如下的窗体,并在窗体上放置一个菜单、一个工具栏控件。菜单内容如第二个图所示。工具栏上有两个按钮,分别对应“打开文本文件”、“保存文本文件”。菜单和工具栏具体功能实现可
- 如何实现一个frame中的按钮事件调用另一个frame的服务器控件
- 自定义Dialog(图片,文字说明,单选按钮)----类ListPreference实现(2)
- 实现一个Asp.net自定义Back控件
- c#+Winform实现自定义的“复制、粘贴”右键快捷菜单,多个控件共享使用一个右键菜单。
- 1 用存储过程实现分页,除了上一页,下一页,第一页,和末页外还要有go按钮,以及go到那里的文本框。另外还要在Lable显示“当前x页,一共y页”。注意验证控件的使用和 链接存储过程的内容。