手把手教你鲁一个属于自己风格的dialog

最近项目里面有个需求,需要自定义一个dialog。废话不多说,直接上代码:

转载注明出处:http://www.jianshu.com/p/d520312e0fe4

个人github链接: https://github.com/       喜欢的小伙伴可以 start 一下

项目开始之前,请确认已经创建了一个完整可以跑的项目工程(如果没有可以自行度娘哦!),完成之后我们的列车就要出发了。






1. 自定义控件,行业规矩继承一个父类控件 

  public classCustomMessageConfirmDialogextendsDialogimplementsView.OnClickListener{


}


2. 重写父类构造方法


public CustomMessageConfirmDialog(Context context){

       this(context,R.style.dialog_costum_stytle);

}

public CustomMessageConfirmDialog(Context context,String title,String content){

         this(context,R.style.dialog_costum_stytle);

}

public CustomMessageConfirmDialog(Context context,int themeResId){

        super(context,themeResId);

        mContext=context;

        this.title=title;

        this.content=content;

}


3.  这里我们传入了一个我们自定义的  style 样式文件,规定一下dialog的样式风格和入场出场动画(当然网上好多人喜欢用代码的形式来处理,这里就不深究了,效果是一样的) 




4. 这里面涉及了两个dialog的动画。一个是入场动画,一个是出场的动画。


Android原生的dialog动画样式。

在参考了Android原生的dialog后,决定对代码进行如下的修改:


4.1  我们同样的在style文件中创建标签为  DialogAnimation  的样式文件,继承父类  parent="android:Animation"

4.2 在标签的内部定义两个子标签,分别是 

需要注意的是这里的  @android:windowEnterAnimation 就是重写了父类的windowEnterAnimation和windowExitAnimation这两个属性


4.3 入场动画引用  @anim/dialog_in  需要在  res —> anim    中定义自己的入场动画  ,文件名为dialog_in.xml ,代码如下:





4.5  同样的出场动画引用了  @anim/dialog_exit  需要在  res —> anim    中定义自己的入场动画  ,文件名为dialog_exit.xml ,代码如下:






到这里动画部分就算完成了。

那我们来看一下,


5. dialog的内容部分    

@Override

protected voidonCreate(BundlesavedInstanceState){

           super.onCreate(savedInstanceState);

           setContentView(R.layout.dialog_message_confirm_custom);

           //初始化view的操作

           initView();



          //不能通过window设置去除标题栏和边框操作,根据源码提示需要通过styles.xml设置

           initListener();

           initData();

}



6.  布局文件

?xml version="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@drawable/bg_round_white"

android:orientation="vertical">



<TextView

android:id="@+id/title"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="center_horizontal"

android:padding="12dp"

android:layout_marginTop="12dp"

android:text="提示"

android:textSize="18sp"

android:textColor="@color/font_common_dialog_title"/>


<TextView

android:id="@+id/content"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="center"

android:layout_gravity="center_horizontal"

android:lineSpacingExtra="3dp"

android:layout_marginLeft="40dp"

android:layout_marginTop="20dp"

android:layout_marginRight="40dp"

android:layout_marginBottom="30dp"

android:text="签到成功,获得200积分"

android:textSize="14sp"

android:textColor="@color/font_common_dialog_context"/>


<TextView

android:layout_width="match_parent"

android:layout_height="1dp"

android:background="@color/commom_background"/>

android:layout_width="match_parent"

android:layout_height="50dp"

android:orientation="horizontal">


<TextView

android:id="@+id/cancel"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@drawable/bg_dialog_left_white"

android:layout_weight="1.0"

android:gravity="center"

android:text="取消"

android:textSize="14sp"

android:textColor="@color/font_color_cancal"/>


<TextView

android:layout_width="1dp"

android:layout_height="match_parent"

android:background="@color/commom_background"/>

android:id="@+id/submit"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@drawable/bg_dialog_right_white"

android:gravity="center"

android:layout_weight="1.0"

android:text="确定"

android:textSize="14sp"

android:textColor="@color/font_color_confirm"/>

</LinearLayout>

</LinearLayout>



7. shrpe文件  绘制边框圆角  (在 drawable中创建  bg_dialog_left_white.xml的文件

<shapexmlns:android="http://schemas.android.com/apk/res/android"

android:shape="rectangle">

<solidandroid:color="@color/white"/>

<cornersandroid:bottomLeftRadius="8dp"/>

</shape>

8.  在textView中引用  android:background="@drawable/bg_dialog_left_white"


色彩值在 values -> colors.xml文件中定义  




到这里完成了








最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 156,757评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,478评论 1 289
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,540评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,593评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,903评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,329评论 1 210
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,659评论 2 309
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,383评论 0 195
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,055评论 1 238
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,337评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,864评论 1 256
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,227评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,820评论 3 231
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,999评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,750评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,365评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,260评论 2 258

推荐阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,189评论 0 17
  • 本人初学Android,最近做了一个实现安卓简单音乐播放功能的播放器,收获不少,于是便记录下来自己的思路与知识总结...
    落日柳风阅读 18,797评论 2 41
  • 一、Welcome延时跳转界面(1s跳转) 二、登陆界面(调用数据库) 输入用户名、密码,点击注册,出现Toast...
    Android课程笔记阅读 1,818评论 0 5
  • 今晚流氓兔推荐的歌曲是《白昼之夜》,纯音乐。听着好舒缓的一首轻音乐,整个人都会放松好多。 突然想问一句:你过得幸福...
    佐恬阅读 212评论 0 0
  • 技术只是工具 金钱也一样, 也只是一种工具 太过重视或太过忽视工具,都是不好的,从哲理上讲,凡事都有一个度,把握好...
    kamfon阅读 124评论 0 0