Android第三十三期 - Dialog的应用
今天遇到一个大难题哦,不过有大牛一眼就瞄出来了,然后就解决了,AlertDialog和Dialog自定义后圆角的处理,如果你跟我一样没有看到这些细节的话就栽了,用AlertDialog不能使得圆角背景透明化,所以要用Dialog处理才行,也就是下面的方法。 Dialog: 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 56 57 58 59 60 61 62 63 64 65 private static DialogmDialog; //加载gridview中的item的xml方法 public static ViewgetView(Contextcontext, int layoutId){ LayoutInflaterinflater=(LayoutInflater)context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); Viewlayout=inflater.inflate(layoutId, null ); return layout; } /** *显示自定义对话框 * *@paramcontext *@parammessage *@paramlistener */ public static void showDialog( final Contextcontext,Stringmessage, final IAlertDialogButtonListenerlistener){ ViewdialogView= null ; //Dialog.Builderbuilder=newBuilder(context, //R.style.Theme_Transparent); mDialog= new Dialog(context,R.style.Theme_Transparent); dialogView=getView(context,R.layout.dialog_view); Buttonbtn_ok=(Button)dialogView.findViewById(R.id.btn_ok); Buttonbtn_cancel=(Button)dialogView.findViewById(R.id.btn_cancel); TextViewtxt_dailog_message=(TextView)dialogView .findViewById(R.id.txt_dailog_message); txt_dailog_message.setText(message); btn_ok.setOnClickListener( new View.OnClickListener(){ @Override public void onClick(Viewarg0){ //关闭dialog if (mDialog!= null ){ mDialog.cancel(); } //事件回调 if (listener!= null ){ listener.onClick(); } //播放音效 //MyPlayer.playTone(context,MyPlayer.INDEX_STONE_ENTER); } }); btn_cancel.setOnClickListener( new View.OnClickListener(){ @Override public void onClick(Viewarg0){ //关闭dialog if (mDialog!= null ){ mDialog.cancel(); } //播放音效 //MyPlayer.playTone(context,MyPlayer.INDEX_STONE_CANCEL); } }); //为dialog设置View //builder.setView(dialogView); mDialog.setContentView(dialogView); //mDialog=builder.create(); mDialog.show(); } } AlertDialog: 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 /** *显示自定义对话框 * *@paramcontext *@parammessage *@paramlistener */ public static void showDialog( final Contextcontext,Stringmessage, final IAlertDialogButtonListenerlistener){ ViewdialogView= null ; AlertDialog.Builderbuilder= new Builder(context, R.style.Theme_Transparent); dialogView=getView(context,R.layout.dialog_view); ImageButtonbtn_ok=(ImageButton)dialogView.findViewById(R.id.btn_ok); ImageButtonbtn_cancel=(ImageButton)dialogView .findViewById(R.id.btn_cancel); TextViewtxt_dailog_message=(TextView)dialogView .findViewById(R.id.txt_dailog_message); txt_dailog_message.setText(message); btn_ok.setOnClickListener( new View.OnClickListener(){ @Override public void onClick(Viewarg0){ //关闭dialog if (mAlertDialog!= null ){ mAlertDialog.cancel(); } //事件回调 if (listener!= null ){ listener.onClick(); } //播放音效 MyPlayer.playTone(context,MyPlayer.INDEX_STONE_ENTER); } }); btn_cancel.setOnClickListener( new View.OnClickListener(){ @Override public void onClick(Viewarg0){ //关闭dialog if (mAlertDialog!= null ){ mAlertDialog.cancel(); } //播放音效 MyPlayer.playTone(context,MyPlayer.INDEX_STONE_CANCEL); } }); //为dialog设置View builder.setView(dialogView); mAlertDialog=builder.create(); mAlertDialog.show(); } af_dialog_background圆角: 1 2 3 4 5 6 7 8 9 10 11 12 <?xmlversion= "1.0" encoding= "utf-8" ?> <shapexmlns:android= "http://schemas.android.com/apk/res/android" > <solidandroid:color= "#3C4856" /> <corners android:bottomLeftRadius= "0.1dp" android:bottomRightRadius= "0.1dp" android:topLeftRadius= "10dp" android:topRightRadius= "10dp" /> </shape> dialog_view: 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 <?xmlversion= "1.0" encoding= "utf-8" ?> <LinearLayoutxmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:gravity= "center_horizontal" android:orientation= "vertical" android:paddingLeft= "10dp" android:paddingRight= "10dp" > <LinearLayout android:layout_width= "match_parent" android:layout_height= "wrap_content" android:background= "@drawable/af_dialog_background" android:orientation= "vertical" > <TextView style= "@style/TextViewStyle_aboutus" android:layout_gravity= "left" android:layout_marginLeft= "10dp" android:layout_marginTop= "5dp" android:text= "@string/af_logo_10_dialog_title" android:textColor= "@color/white4" android:textSize= "14sp" /> <ScrollView android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_marginLeft= "10dp" android:layout_marginRight= "10dp" android:layout_marginTop= "10dp" android:background= "@drawable/af_dialog_background" android:fillViewport= "true" android:scrollbars= "none" > <TextView android:id= "@+id/txt_dailog_message" style= "@style/TextViewStyle_aboutus" android:lineSpacingMultiplier= "1.2" android:text= "@string/af_logo_10_dialog_content" android:textColor= "@color/white4" android:textSize= "14sp" /> </ScrollView> </LinearLayout> <LinearLayout android:layout_width= "match_parent" android:layout_height= "wrap_content" android:background= "@color/white" android:gravity= "center_horizontal" android:orientation= "horizontal" > <Button android:id= "@+id/btn_cancel" style= "?android:attr/buttonBarButtonStyle" android:layout_width= "0dp" android:layout_height= "wrap_content" android:layout_marginBottom= "5dp" android:layout_marginLeft= "25dp" android:layout_marginRight= "15dp" android:layout_marginTop= "5dp" android:layout_weight= "1" android:background= "@drawable/btn_cancelclick" android:contentDescription= "@string/action_settings" android:paddingBottom= "8dp" android:paddingTop= "8dp" android:text= "@string/af_cancel" android:textColor= "@color/white" android:textSize= "16sp" /> <Button android:id= "@+id/btn_ok" style= "?android:attr/buttonBarButtonStyle" android:layout_width= "0dp" android:layout_height= "wrap_content" android:layout_marginBottom= "5dp" android:layout_marginLeft= "15dp" android:layout_marginRight= "25dp" android:layout_marginTop= "5dp" android:layout_weight= "1" android:background= "@drawable/btn_able" android:contentDescription= "@string/action_settings" android:paddingBottom= "8dp" android:paddingTop= "8dp" android:text= "@string/af_confirm" android:textColor= "@color/white" android:textSize= "16sp" /> </LinearLayout> </LinearLayout> 最后是style: 1 2 3 4 5 6 7 8 9 10 <stylename= "Theme_Transparent" parent= "@android:style/Theme.Dialog" > <itemname= "android:windowFrame" > @null </item> <itemname= "android:windowIsFloating" > true </item> <itemname= "android:windowIsTranslucent" > true </item> <itemname= "android:windowNoTitle" > true </item> <itemname= "android:background" > @android :color/transparent</item> <itemname= "android:windowBackground" > @android :color/transparent</item> <itemname= "android:backgroundDimEnabled" > true </item> <itemname= "android:backgroundDimAmount" > 0.6 </item> </style> 谢谢hongyang大神~~开心挣钱每一天 本文转自 吴雨声 51CTO博客,原文链接:http://blog.51cto.com/liangxiao/1613291,如需转载请自行联系原作者