android 自定义带关闭按钮的dialog   在android的开发过程中,使用android常用的那几种dialog样式是远远不够的,不符合设计给出的设计图,尤其是右上角带有关闭按钮的dialog,之前一直都没有太在意这方面的,但是在实际的项目中,遇到了,上网查了一些资料,但是很少,而且觉得有点复杂,因此按照自己的理解实现了,效果图如下: android 自定义带关闭按钮的dialog_第1张图片

主要是在自定义的dialog.xml布局文件上,多的不说了,直接上代码吧:
  1、dialog.xml
<?xml version="1.0" encoding="utf-8"?>
            android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:background="@android:color/transparent">


                    android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" 
            android:layout_margin="10dip"
            android:background="#ffffff">


                            android:id="@+id/textView1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="文字描述" 
                 android:layout_margin="10dip"/>


                            android:id="@+id/textView2"
                android:layout_width="fill_parent"
                android:layout_height="0.25dp"
               android:background="#cccccc"/>


                            android:layout_width="match_parent"
                android:layout_height="150dip" >


                                    android:id="@+id/ratingBar1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true" />


                                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/ratingBar1"
                    android:layout_centerHorizontal="true"
                    android:text="这是进度条" />


           


       
        
                          android:id="@+id/close"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/close"
                android:layout_gravity="top|right" />

   


 2、style样式

   


 3、自定义的dialog类MyDialog.xml
package com.lly.cn;


import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;


public class MyDialog extends Dialog{
ImageView img_back;
Context context;
public MyDialog(Context context, int theme) {
super(context, theme);
// TODO Auto-generated constructor stub
this.context=context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.dialog);

initview();
}
private void initview() {
// TODO Auto-generated method stub
img_back=(ImageView) findViewById(R.id.close);
img_back.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
MyDialog.this.dismiss();
}
});
}
}

 4、自定义dialog的使用
只要在你想用的地方加上这几句就可以         mydialog=new MyDialog(MainActivity.this,R.style.MyDialog);
mydialog.show();
mydialog.setCancelable(false);   //点击dialog以外的地方dialog不消失,可根据自己的需要来改咯

这样就可以完成自定义带关闭按钮的dialog了,希望对你有用!!

源码下载

更多相关文章

  1. Android自定义控件——仿ios开关按钮
  2. Android -- Button(按钮)的几种监听方式
  3. android中listview的一些样式设置,自定义ListView行间的分割线
  4. Android 样式系统 | 主题背景和样式
  5. android设置控件样式(边框颜色,圆角)和图片样式(圆角)
  6. android上多样式文本的使用
  7. android 按钮的四种点击事件
  8. Android - 保持在底部的按钮栏,上面是滚动的ScrollView

随机推荐

  1. Android之短信发送器
  2. Linux 命令行更新指定版本 android sdk
  3. Android(安卓)调用系统浏览器 出现activi
  4. android API——EditText
  5. 1、ListView自定义控件下拉刷新(一)
  6. android 测试简介
  7. Android(安卓)开发中遇到的 bug(3)
  8. Android应用程序安装过程源代码分析(4)
  9. Android软键盘弹出,界面整体上移的问题
  10. Android(安卓)学习笔记--android――Noti