当前位置:   article > 正文

Android自定义Dialog_android 自定义dialog

android 自定义dialog

转载请标明出处: 
http://blog.csdn.net/android_it/article/details/51161038 
本文出自:【老甩哥的CSDN博客】

在我们开发app的时候,很多地方需要弹出一个对话框,我们要不就直接用系统的Dialog或者就是AlertDialog,但是美工给我们的效果图片很多都是无法去实现的。接下来我们来看下自定义Dialog的使用方法:首先我给大家展示2个图片: 
这里写图片描述 这里写图片描述

上面的2组图片使我们开发app经常需要的弹窗展示,四周是圆角,iOS几乎都是这个效果,里面的文字信息,颜色都可以改变。接下来我们逐一的进行讲解怎么去自定义Dialog做出这样子的效果: 
一:首先我们要在values文件styles下,来写dialog的风格:

  1. <!-- dialog样式 -->
  2. <style name="dialog_custom" parent="@android:style/Theme.Dialog">
  3. <item name="android:windowIsFloating">true</item> <!--是否浮在界面上-->
  4. <item name="android:windowIsTranslucent">true</item> <!--是否半透明-->
  5. <item name="android:windowNoTitle">false</item> <!--是否有标题-->
  6. <item name="android:windowBackground">@android:color/transparent</item> <!--窗口背景色透明-->
  7. <item name="android:backgroundDimEnabled">false</item> <!--背景是否模糊显示-->
  8. </style>
  9. <!-- dialog底部弹出菜单动画 -->
  10. <style name="bottom_menu_animation" parent="android:Animation">
  11. <item name="@android:windowEnterAnimation">@anim/bottom_menu_enter</item>
  12. <item name="@android:windowExitAnimation">@anim/bottom_menu_exit</item>
  13. </style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

大家可以看到我在styles里面还加了一个动画效果风格,这个在后面自定义Dialog的时候会用到。@anim/bottom_menu_enter和@anim/bottom_menu_exit 分别是在res下建一个anim包添加了2个文件bottom_menu_enter和bottom_menu_exit : 
在bottom_menu_enter下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <translate
  4. android:duration="400"
  5. android:fromYDelta="100%p" />
  6. </set>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

在bottom_menu_exit 下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <translate
  4. android:duration="600"
  5. android:toYDelta="100%p" />
  6. </set>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

上面的动画我就不在阐述了,继续朝下说: 
首先我们先想下,自定义一个Dialog,首先要继承Dialog,然后就是要有构造方法,然后重写里面的某些方法,其实就是这样子。如果我们在某个Activity或者Fragment里面想要这个自定义Dialog的话,那么我们就要new这个自定义的Dialog。 
那么我们就很自然的想到我们需要一个Context上下文,一个布局文件,如果我们还要操作布局里面的文件的话,那我们还要布局文件里面的id和监听事件; 
说到这里:我们就开始一步一步的去写这个自定义的文件: 
我们起一个自定义Dialog叫CenterDialog:

  1. private Context context; // 上下文
  2. private int layoutResID; // 布局文件id
  3. private int[] listenedItems; // 要监听的控件id
  4. public CenterDialog(Context context, int layoutResID, int[] listenedItems) {
  5. super(context, R.style.dialog_custom); //dialog的样式
  6. this.context = context;
  7. this.layoutResID = layoutResID;
  8. this.listenedItems = listenedItems;
  9. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

通过上面的分析,我们已经把Context,布局文件,布局文件里面的控件id还有构造方法都写好了,这里说下,因为布局文件里面的id控件会有很多,所有写了一个int[]数组。

接下来我们重写onCreate()方法:

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. Window window = getWindow();
  5. window.setGravity(Gravity.CENTER); // 此处可以设置dialog显示的位置为居中
  6. window.setWindowAnimations(R.style.bottom_menu_animation); // 添加动画效果
  7. setContentView(layoutResID);
  8. WindowManager windowManager = ((Activity) context).getWindowManager();
  9. Display display = windowManager.getDefaultDisplay();
  10. WindowManager.LayoutParams lp = getWindow().getAttributes();
  11. lp.width = display.getWidth()*4/5; // 设置dialog宽度为屏幕的4/5
  12. getWindow().setAttributes(lp);
  13. setCanceledOnTouchOutside(true);// 点击Dialog外部消失
  14. //遍历控件id,添加点击事件
  15. for (int id : listenedItems) {
  16. findViewById(id).setOnClickListener(this);
  17. }
  18. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

接下来就是让CenterDialog implements View.OnClickListener重写onClick()方法,到这里我们再想下,如果想在外部要监听布局文件控件的事件,首先我们要对CenterDialog添加监听事件,然后才可以进行控制控件的监听事件?怎么做呢? 
如果Java学的好的话,很明显我们要在这里面写个接口,然后添加一个方法,让外部重写,那下面我们看下代码:

  1. private OnCenterItemClickListener listener;
  2. public interface OnCenterItemClickListener {
  3. void OnCenterItemClick(CenterDialog dialog, View view);
  4. }
  5. public void setOnCenterItemClickListener(OnCenterItemClickListener listener) {
  6. this.listener = listener;
  7. }
  8. @Override
  9. public void onClick(View view) {
  10. dismiss();//注意:我在这里加了这句话,表示只要按任何一个控件的id,弹窗都会消失,不管是确定还是取消。
  11. listener.OnCenterItemClick(this, view);
  12. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

好了,自定义的CenterDialog已经书写完毕,那我们调用看看: 
首先写个简单的布局:activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="#F0EFF5">
  6. <Button
  7. android:id="@+id/button"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="点击" />
  11. </LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

然后再写个dialog布局:dialog_layout.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@drawable/dialog_center_background"
  6. android:gravity="center_horizontal"
  7. android:orientation="vertical">
  8. <TextView
  9. android:id="@+id/dialog_text"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:layout_marginBottom="20dp"
  13. android:layout_marginTop="20dp"
  14. android:text="需要写的信息"
  15. android:textColor="#F88833"
  16. android:textSize="20sp" />
  17. <View
  18. android:layout_width="match_parent"
  19. android:layout_height="1dp"
  20. android:background="#cecece" />
  21. <LinearLayout
  22. android:layout_width="match_parent"
  23. android:layout_height="50dp"
  24. android:orientation="horizontal">
  25. <TextView
  26. android:id="@+id/dialog_cancel"
  27. android:layout_width="0dp"
  28. android:layout_height="match_parent"
  29. android:layout_weight="1"
  30. android:gravity="center"
  31. android:text="取消"
  32. android:textColor="#6FBF6A"
  33. android:textSize="20sp" />
  34. <View
  35. android:layout_width="1dp"
  36. android:layout_height="match_parent"
  37. android:background="#cecece" />
  38. <TextView
  39. android:id="@+id/dialog_sure"
  40. android:layout_width="0dp"
  41. android:layout_height="match_parent"
  42. android:layout_weight="1"
  43. android:gravity="center"
  44. android:text="确定"
  45. android:textColor="#6FBF6A"
  46. android:textSize="20sp" />
  47. </LinearLayout>
  48. </LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

在MainActivity里面:我们new出CenterDialog对象,并添加点击事件:

  1. package com.fshsoft.dialogdemo;
  2. import android.support.v7.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Button;
  6. public class MainActivity extends AppCompatActivity implements View.OnClickListener, CenterDialog.OnCenterItemClickListener {
  7. private Button button;
  8. private CenterDialog centerDialog;
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_main);
  13. button = (Button) findViewById(R.id.button);
  14. button.setOnClickListener(this);
  15. centerDialog = new CenterDialog(this, R.layout.dialog_layout,
  16. new int[]{R.id.dialog_cancel, R.id.dialog_sure});
  17. centerDialog.setOnCenterItemClickListener(this);
  18. }
  19. @Override
  20. public void onClick(View v) {
  21. centerDialog.show();
  22. }
  23. @Override
  24. public void OnCenterItemClick(CenterDialog dialog, View view) {
  25. case R.id.dialog_sure:
  26. Toast.makeText(MainActivity.this,"确定按钮",Toast.LENGTH_SHORT).show();
  27. break;
  28. default:
  29. break;
  30. }
  31. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

在OnCenterItemClick();里面我没有进行取消按钮的判断,是因为在自定义的CenterDialog里面我已经把所有控件的id,点击都dismiss了,前面已经提到了。

以上就是自定义dialog的内容,到这里,也许你会说,我的布局里面需要一个取消按钮,但是不是确定,是添加图片这个字,我不可能再去写个布局把,你说的对,我们可以这样子做:

  1. new int[]{R.id.dialog_cancel, R.id.dialog_sure});
  2. centerDialog.show();
  3. TextView dialog_sure = (TextView) centerDialog.findViewById(R.id.dialog_sure);
  4. dialog_sure.setText("添加图片");
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

这里需要注意的事就是,需要先把dialog给show()出来,然后通过centerDialog.findViewById(R.id.dialog_sure);才能拿到控件对象,然后在setText(“添加图片”);当然还可以改变字体的颜色等等信息,这里就不再列举。

以上就是全部内容,写到这里,你可以尝试写下第二张图片的dialog.

不足之处请留言指正!有问题的可以给我留言!谢谢!

下载源码

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号