透明自定义对话框以及进度对话框的实现
来源:互联网 发布:精微工资软件下载 编辑:程序博客网 时间:2024/05/16 18:58
来源于:http://blog.csdn.net/zoeice/article/details/7699921
项目中经常会用到对话框,但是系统的对话框又比较难看,因此就来实现自定义对话框。
办法很简单,就是定义一个Dialog,并自定义它的布局以及样式:
在res/layout里定义一个slt_cnt_type.xml,代码如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/bgDialog"
- android:layout_width="240dp"
- android:layout_height="wrap_content"
- android:background="#0000"
- android:orientation="vertical" >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center_vertical" >
- <ImageView
- android:id="@+id/ImageView01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/messagebox_warning" />
- <TextView
- android:id="@+id/title01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/title"
- android:textColor="#ffe9feff"
- android:textSize="28px" />
- </LinearLayout>
- <ScrollView
- android:id="@+id/ScrollView01"
- android:layout_width="wrap_content"
- android:layout_height="140px"
- android:background="#0000" >
- <TextView
- android:id="@+id/TextView01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#0000"
- android:textColor="#ffe9feff" >
- </TextView>
- </ScrollView>
- <Button
- android:id="@+id/ly1btn1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/bn9"
- android:text="Cancel"
- android:gravity="center_horizontal" >
- </Button>
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <style name="dialog" parent="@android:style/Theme.Dialog">
- <!-- 更换背景图片实现全透明 -->
- <item name="android:windowBackground">@drawable/alert_light</item>
- <!-- 屏幕背景不变暗 -->
- <item name="android:backgroundDimEnabled">false</item>
- <!-- 隐藏标题 -->
- <item name="android:windowNoTitle">true</item>
- </style>
- </resources>
然后再你需要弹出的对话框的地方加上:
- /* 初始化普通对话框。并设置样式 */
- Dialog selectDialog = new Dialog(DialogTest.this, R.style.dialog);
- selectDialog.setCancelable(true);
- /* 设置普通对话框的布局 */
- selectDialog.setContentView(R.layout.slt_cnt_type);
- /* +2+取得布局中的文本控件,并赋值需要显示的内容+2+ */
- TextView textView01 = (TextView) selectDialog
- .findViewById(R.id.TextView01);
- textView01
- .setText("光阴似箭,日月如梭。譬如流水,不舍昼夜。时光匆匆,岁月悠悠。红尘过客,流年几许。前世今生,春去秋来。愿君惜时,比肩圣人。");
- btnItem1 = (Button) selectDialog.findViewById(R.id.ly1btn1);
- btnItem1.setOnClickListener(new Button.OnClickListener() {
- @Override
- public void onClick(View view) {
- selectDialog.dismiss();//隐藏对话框
- }
- });
- selectDialog.show();//显示对话框
这样就完成了,是不是很简单。
然后我们实现自定义进度对话框(这里为了简单清晰,就不涉及百分比了):
先在res/layout里添加进度对话框布局文件
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/layout_root"
- android:layout_width="200px"
- android:layout_height="120px"
- android:layoutAnimation="@anim/dialog_anim_layout"
- android:orientation="horizontal" >
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/bn9"
- android:gravity="center" >
- <!-- 图片切换器 -->
- <ImageSwitcher
- android:id="@+id/image"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@anim/plane_progress" />
- </LinearLayout>
- </LinearLayout>
然后在res/anim定义图片切换器的帧图片:
- <?xml version="1.0" encoding="utf-8"?>
- <!-- 指定动画循环播放 oneshot设置是否循环-->
- <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
- android:oneshot="false">
- <!-- 添加多个帧 -->
- <item android:drawable="@drawable/cloud4" android:duration="60" />
- <item android:drawable="@drawable/cloud3" android:duration="60" />
- <item android:drawable="@drawable/cloud2" android:duration="60" />
- <item android:drawable="@drawable/cloud1" android:duration="60" />
- <item android:drawable="@drawable/cloud" android:duration="60" />
- </animation-list>
最后在代码中定义进度对话框:
- /**
- * 进度对话框
- * @author ZOEICER
- */
- public class DialogProgress extends AlertDialog{
- private Handler handler = new Handler();
- /**动画*/
- AnimationDrawable anim;
- /**图片切换器*/
- ImageSwitcher imageSwitcher;
- public DialogProgress(Context context) {
- super(context);
- // TODO Auto-generated constructor stub
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.dialogprogress);
- imageSwitcher = (ImageSwitcher)findViewById(R.id.image);
- handler.postDelayed(new Runnable(){
- @Override
- public void run()
- {
- /*动画为图片切换器的帧动画*/
- anim = (AnimationDrawable)imageSwitcher.getBackground();
- anim.start();//开始动画
- }
- }, 500);
- }
- @Override
- public void dismiss() {
- // TODO Auto-generated method stub
- super.dismiss();
- anim.stop();//停止动画
- }
- }
然后调用:
- DialogProgress dp = new DialogProgress(DialogTest.this);
- dp.show();//显示对话框
下面附上源文件:
http://download.csdn.net/detail/zoeice/4399354
0 0
- 透明自定义对话框以及进度对话框的实现
- 透明自定义对话框以及进度对话框的实现
- 十七、自定义进度对话框
- 自定义进度动画对话框
- VC实现对话框的透明
- dojo实现透明对话框以及拖动效果
- 如何实现进度对话框
- Android对话框的自定义及透明效果
- Android对话框的自定义及透明效果
- Android自定义对话框实现以及style的应用
- android 自定义对话框 背景透明
- android 自定义对话框 背景透明
- Android自定义对话框的实现
- 自定义对话框的实现--Android
- Android自定义全透明Dalog对话框以及自定义全透明Taost弹出框+对话框背景模糊处理
- 各种对话框的实现(文字对话框,选择对话框,弹出输入对话框,单选,多选对话框,自定义对话框)
- 进度对话框
- MFC实现对话框透明显示:对话框透明而上面的文字不透明
- 第十三周 项目5(1):统计每个数字字符出现的次数
- 13周项目一。。删除数组中的数
- 娱乐化手游营销模式探索
- (android高仿系列)今日头条 --新闻阅读器 (三) 完结 、总结 篇
- 浮点数字符串转换成浮点数实现
- 透明自定义对话框以及进度对话框的实现
- java算法之排序
- Ubuntu Eclipse使用经验(一)
- Memcached 集群部署
- ios5 ios6内存警告
- UIGraphicsBeginImageContext系列知识
- eclipse jar 打包
- Session和Cookie
- mac更改管理员密码