本文共 7313 字,大约阅读时间需要 24 分钟。
今天遇到一个大难题哦,不过有大牛一眼就瞄出来了,然后就解决了,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 Dialog mDialog; // 加载gridview中的item的xml方法 public static View getView(Context context, int layoutId) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(layoutId, null ); return layout; } /** * 显示自定义对话框 * * @param context * @param message * @param listener */ public static void showDialog( final Context context, String message, final IAlertDialogButtonListener listener) { View dialogView = null ; // Dialog.Builder builder = new Builder(context, // R.style.Theme_Transparent); mDialog = new Dialog(context, R.style.Theme_Transparent); dialogView = getView(context, R.layout.dialog_view); Button btn_ok = (Button) dialogView.findViewById(R.id.btn_ok); Button btn_cancel = (Button) dialogView.findViewById(R.id.btn_cancel); TextView txt_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(View arg0) { // 关闭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(View arg0) { // 关闭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 | /** * 显示自定义对话框 * * @param context * @param message * @param listener */ public static void showDialog( final Context context, String message, final IAlertDialogButtonListener listener) { View dialogView = null ; AlertDialog.Builder builder = new Builder(context, R.style.Theme_Transparent); dialogView = getView(context, R.layout.dialog_view); ImageButton btn_ok = (ImageButton) dialogView.findViewById(R.id.btn_ok); ImageButton btn_cancel = (ImageButton) dialogView .findViewById(R.id.btn_cancel); TextView txt_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(View arg0) { // 关闭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(View arg0) { // 关闭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 | <?xml version= "1.0" encoding= "utf-8" ?> <shape xmlns:android= "http://schemas.android.com/apk/res/android" > <solid android: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 | <?xml version= "1.0" encoding= "utf-8" ?> <LinearLayout xmlns: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 | <style name= "Theme_Transparent" parent= "@android:style/Theme.Dialog" > <item name= "android:windowFrame" > @null </item> <item name= "android:windowIsFloating" > true </item> <item name= "android:windowIsTranslucent" > true </item> <item name= "android:windowNoTitle" > true </item> <item name= "android:background" > @android :color/transparent</item> <item name= "android:windowBackground" > @android :color/transparent</item> <item name= "android:backgroundDimEnabled" > true </item> <item name= "android:backgroundDimAmount" > 0.6 </item> </style> |
谢谢hongyang大神~~开心挣钱每一天