首页 > 代码库 > Android 自定义Dialog
Android 自定义Dialog
1、drawable
1 <shape xmlns:android="http://schemas.android.com/apk/res/android" > 2 <corners android:radius="10dp"/> 3 4 </shape>
1 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 2 <corners 3 android:topLeftRadius="10dp" 4 android:topRightRadius="10dp"/> 5 6 <solid android:color="#DEDBDE"/> 7 8 </shape>
1 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 2 <corners 3 android:bottomLeftRadius="10dp" 4 android:bottomRightRadius="10dp"/> 5 6 <solid 7 android:color="@android:color/white"/> 8 9 </shape>
1 <shape xmlns:android="http://schemas.android.com/apk/res/android" > 2 <corners android:radius="50dp"/> 3 4 <solid android:color="@android:color/holo_orange_light"/> 5 6 </shape>
1 <shape xmlns:android="http://schemas.android.com/apk/res/android" > 2 <corners android:radius="50dp"/> 3 4 <solid android:color="@android:color/darker_gray"/> 5 6 </shape>
2、
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:orientation="vertical" > 5 6 <Button 7 android:id="@+id/btn_test" 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content" 10 android:onClick="alertDialog" 11 android:text="测试点击打开Dialog" /> 12 13 </LinearLayout>
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:id="@+id/rl" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="@android:color/darker_gray" > 6 7 <LinearLayout 8 android:id="@+id/ll" 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 android:layout_centerInParent="true" 12 android:layout_marginLeft="50dp" 13 android:layout_marginRight="50dp" 14 android:background="@drawable/dialog_bg" 15 android:gravity="center" 16 android:orientation="vertical" > 17 18 <TextView 19 android:layout_width="match_parent" 20 android:layout_height="wrap_content" 21 android:background="@drawable/top_style" 22 android:gravity="center" 23 android:padding="10dp" 24 android:text="提示" 25 android:textSize="20sp" /> 26 27 <TextView 28 android:layout_width="match_parent" 29 android:layout_height="wrap_content" 30 android:background="@android:color/white" 31 android:gravity="center" 32 android:padding="20dp" 33 android:text="发现新版本" 34 android:textSize="18sp" /> 35 36 <TextView 37 android:layout_width="match_parent" 38 android:layout_height="wrap_content" 39 android:background="@android:color/white" 40 android:gravity="center" 41 android:padding="20dp" 42 android:text="v3.5.0 → v3.5.1" 43 android:textSize="18sp" /> 44 45 <LinearLayout 46 android:layout_width="match_parent" 47 android:layout_height="wrap_content" 48 android:background="@drawable/bottom_style" 49 android:gravity="center_horizontal" 50 android:orientation="horizontal" > 51 52 <Button 53 android:id="@+id/negative" 54 android:layout_width="0dp" 55 android:layout_height="wrap_content" 56 android:layout_margin="20dp" 57 android:layout_weight="1" 58 android:background="@drawable/cancel" 59 android:gravity="center" 60 android:padding="5dp" 61 android:text="取 消" 62 android:textColor="@android:color/white" 63 android:textSize="18sp" /> 64 65 <Button 66 android:id="@+id/positive" 67 android:layout_width="0dp" 68 android:layout_height="wrap_content" 69 android:layout_margin="20dp" 70 android:layout_weight="1" 71 android:background="@drawable/update" 72 android:gravity="center" 73 android:padding="5dp" 74 android:text="更 新" 75 android:textColor="@android:color/white" 76 android:textSize="18sp" /> 77 </LinearLayout> 78 </LinearLayout> 79 80 </RelativeLayout>
3、Dialog主题 values/styles
1 <style name="DialogTheme"> 2 <item name="android:windowNoTitle">true</item> 3 <item name="android:windowBackground">@android:color/transparent</item> 4 <item name="android:windowIsFloating">false</item> 5 </style>
4、MyDialog继承Dialog
1 public class MyDialog extends Dialog { 2 3 private RelativeLayout rl; 4 private LinearLayout ll; 5 6 private Button positive,negative; 7 private String positiveButtonText,negativeButtonText; 8 private DialogInterface.OnClickListener positiveButtonClickListener; 9 private DialogInterface.OnClickListener negativeButtonClickListener; 10 11 public MyDialog(Context context) { 12 super(context, R.style.DialogTheme); 13 } 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.dialog); 19 initView(); 20 } 21 22 //提供给外部访问设置 23 public void setPositiveButton(String positiveButtonText, DialogInterface.OnClickListener listener){ 24 this.positiveButtonText = positiveButtonText; 25 this.positiveButtonClickListener = listener; 26 } 27 28 public void setNegativeButton(String negativeButtonText, DialogInterface.OnClickListener listener){ 29 this.negativeButtonText = negativeButtonText; 30 this.negativeButtonClickListener = listener; 31 } 32 33 private void initView() { 34 // 设置点击Dialog以外的地方可以关闭Dialog 35 //本次测试需要自定义效果,暂且设置不浮动,Dialog占满全屏,使用相对布局,Dialog可见内容部分居中,点击之外的部分可关闭对话框 36 initCanceledOnTouchOutside(); 37 initPositiveButton(); 38 initNegativeButton(); 39 } 40 41 //初始化positiveButton,当外部调用setPositiveButton时,传递positiveButtonText,并实现接口,回调onClick方法 42 private void initPositiveButton() { 43 if(positiveButtonText!=null){ 44 positive = (Button) findViewById(R.id.positive); 45 positive.setText(positiveButtonText); 46 } 47 if(positiveButtonClickListener!=null){ 48 //外部不自己设置就直接使用xml中默认的 49 positive = (Button) findViewById(R.id.positive); 50 positive.setOnClickListener(new View.OnClickListener() { 51 @Override 52 public void onClick(View v) { 53 positiveButtonClickListener.onClick(MyDialog.this, DialogInterface.BUTTON_POSITIVE); 54 } 55 }); 56 } 57 } 58 59 private void initNegativeButton() { 60 if(negativeButtonText!=null){ 61 negative = (Button) findViewById(R.id.negative); 62 negative.setText(negativeButtonText); 63 } 64 if(negativeButtonClickListener!=null){ 65 //外部不自己设置就直接使用xml中默认的 66 negative = (Button) findViewById(R.id.negative); 67 negative.setOnClickListener(new View.OnClickListener() { 68 @Override 69 public void onClick(View v) { 70 negativeButtonClickListener.onClick(MyDialog.this, DialogInterface.BUTTON_NEGATIVE); 71 } 72 }); 73 } 74 } 75 76 77 private void initCanceledOnTouchOutside() { 78 rl = (RelativeLayout) findViewById(R.id.rl); 79 ll = (LinearLayout) findViewById(R.id.ll); 80 81 rl.setOnTouchListener(new OnTouchListener() { 82 @Override 83 public boolean onTouch(View v, MotionEvent event) { 84 dismiss();// 关闭对话框 85 return false; 86 } 87 }); 88 ll.setOnClickListener(new View.OnClickListener() { 89 90 @Override 91 public void onClick(View v) { 92 93 } 94 95 }); 96 } 97 }
5、MainActivity
1 public class MainActivity extends Activity { 2 3 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.activity_main); 8 } 9 10 public void alertDialog(View v){ 11 MyDialog dialog = new MyDialog(this); 12 13 dialog.setPositiveButton("更新你妹", new DialogInterface.OnClickListener() { 14 @Override 15 public void onClick(DialogInterface dialog, int which) { 16 //设置你的操作,这里只做关闭对对话框操作 17 dialog.dismiss(); 18 } 19 }); 20 21 dialog.setNegativeButton("取消你弟", new DialogInterface.OnClickListener() { 22 @Override 23 public void onClick(DialogInterface dialog, int which) { 24 //设置你的操作,这里只做关闭对话框操作 25 dialog.dismiss(); 26 27 } 28 }); 29 30 dialog.show(); 31 //使用这个函数设置,必须在自定义Dialog的主题中设置name="android:windowIsFloating">true,否则Dialog将使用全屏 32 //本次测试需要自定义效果,暂且设置不浮动,Dialog占满全屏,使用相对布局,Dialog可见内容部分居中,点击之外的部分可关闭对话框 33 //diaolg.setCanceledOnTouchOutside(true); 34 35 36 37 } 38 }
Android 自定义Dialog
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。