仿IOS圓角可自定義擴展對話框

因爲項目需要,所以寫了一個圓角對話框庫,可以自定義圓角半徑,view和按鈕顯示等內容。

1.效果截圖

在這裏插入圖片描述
在這裏插入圖片描述

新版本更新:

1.增加RCDHTextOption進行更豐富的文本控件屬性配置
2.增加對話框setCancelable屬性設置
3.增加對話框入場出場動畫、大小設置
4.優化項目框架,提供代理類配置
5.解耦代碼邏輯,優化處理效果

2.添加依賴

1.Add it in your root build.gradle at the end of repositories:

	allprojects {
		repositories {
			...
			maven { url 'https://www.jitpack.io' }
		}
	}

2.Add the dependency

dependencies {
		implementation 'com.github.MingYueChunQiu:RoundCornerDialogHelper:0.2.2'
	}

3.項目使用

1.配置屬性

    private float leftTopCornerRadius;//左上圓角半徑
    private float rightTopCornerRadius;//右上圓角半徑
    private float leftBottomCornerRadius;//左下圓角半徑
    private float rightBottomCornerRadius;//右下圓角半徑
    private float cornerRadius;//圓角半徑
    private int bgColor = Color.WHITE;//背景顏色(默認爲白色)
    private Drawable bgDrawable;//背景圖片
    private String titleText;//標題文本
    private int titleTextColor;//標題文本顏色
    private int titleTextSize;//標題文本大小
    private int titlePadding;//標題內邊距
    private int titleTextAppearance;//標題文本樣式(默認先使用樣式,單獨設置的顏色大小會覆蓋掉樣式中內容)
    private int titleBgColor;//標題背景顏色
    private int titleGravity;//標題對齊方式
    private String contentText;//內容文本
    private int contentTextColor;//內容文本顏色
    private int contentTextSize;//內容文本大熊啊
    private int contentPadding;//內容內邊距
    private int contentTextAppearance;//內容文本樣式(默認先使用樣式,單獨設置的顏色大小會覆蓋掉樣式中內容)
    private int contentBgColor;//內容背景顏色
    private int contentGravity;//內容文本對齊方式
    private String leftButtonText;//左邊按鈕文本
    private int leftButtonTextColor;//左邊按鈕文本顏色
    private int leftButtonTextSize;//左邊按鈕文本大小
    private int leftButtonPadding;//左邊按鈕內邊距
    private int leftButtonTextAppearance;//左邊按鈕文本樣式(默認先使用樣式,單獨設置的顏色大小會覆蓋掉樣式中內容)
    private int leftButtonBgColor;//左邊按鈕背景顏色
    private int leftButtonGravity;//左邊按鈕對齊方式
    private String middleButtonText;//中間按鈕文本
    private int middleButtonTextColor;//中間按鈕文本顏色
    private int middleButtonTextSize;//中間按鈕文本大小
    private int middleButtonPadding;//中間按鈕內邊距
    private int middleButtonTextAppearance;//中間按鈕文本樣式(默認先使用樣式,單獨設置的顏色大小會覆蓋掉樣式中內容)
    private int middleButtonBgColor;//中間按鈕背景顏色
    private int middleButtonGravity;//中間按鈕對齊方式
    private String rightButtonText;//右邊按鈕文本
    private int rightButtonTextColor;//右邊按鈕文本顏色
    private int rightButtonTextSize;//右邊按鈕文本大小
    private int rightButtonPadding;//右邊按鈕內邊距
    private int rightButtonTextAppearance;//右邊按鈕文本樣式(默認先使用樣式,單獨設置的顏色大小會覆蓋掉樣式中內容)
    private int rightButtonBgColor;//右邊按鈕背景顏色
    private int rightButtonGravity;//右邊按鈕對齊方式
    private boolean isTitleVisible = true;//標題是否可見
    private boolean isContentVisible = true;//內容是否可見
    private RCDHTextOption titleTextOption;//標題文本配置項(推薦,默認先使用,單獨設置的屬性會覆蓋TextOption)
    private RCDHTextOption contentTextOption;//內容文本配置項(推薦,默認先使用,單獨設置的屬性會覆蓋TextOption)
    private RCDHTextOption leftButtonTextOption;//左邊按鈕文本配置項(推薦,默認先使用,單獨設置的屬性會覆蓋TextOption)
    private RCDHTextOption middleButtonTextOption;//中間按鈕文本配置項(推薦,默認先使用,單獨設置的屬性會覆蓋TextOption)
    private RCDHTextOption rightButtonTextOption;//右邊按鈕文本配置項(推薦,默認先使用,單獨設置的屬性會覆蓋TextOption)
    private CustomViewable customViewable;//自定義顯示view
    private CustomButtonContainerViewable customButtonContainerView;//自定義按鈕容器類
    //點擊左邊按鈕監聽器,設置監聽器後按鈕纔會顯示
    private OnRCDHClickLeftButtonListener onRCDHClickLeftButtonListener;
    //點擊中間按鈕監聽器,設置監聽器後按鈕纔會顯示
    private OnRCDHClickMiddleButtonListener onRCDHClickMiddleButtonListener;
    //點擊右邊按鈕監聽器,設置監聽器後按鈕纔會顯示
    private OnRCDHClickRightButtonListener onRCDHClickRightButtonListener;
    private int width;//對話框寬
    private int height;//對話框高
    private boolean isCancelable = true;//標記是否可以外部點擊取消對話框
    private int windowAnimationStyleResId;//窗口動畫樣式
    private RCDHOptionDelegateable delegate;//配置信息處理的代理類

2.文本配置信息類

private int gravity;//對齊方式
    private int topMargin;//上外邊距
    private int bottomMargin;//下外邊距
    private int leftMargin;//左外邊距
    private int rightMargin;//右外邊距
    private int margin;//外邊距(其餘外邊距失效)
    private int topPadding;//上內邊距
    private int bottomPadding;//下內邊距
    private int leftPadding;//左內邊距
    private int rightPadding;//右內邊距
    private int padding;//內邊距(其餘內邊距失效)
    private String text;//文本
    private int textColor;//文本顏色
    private int textSize;//文本大小
    private int textAppearanceResId;//文本樣式資源ID(先設置樣式,單獨設置的顏色大小會覆蓋掉樣式中內容)

3.創建對話框

RoundCornerDialogFragment fragment = RoundCornerDialogFragment.newInstance();
fragment.show(getSupportFragmentManager(), RoundCornerDialogFragment.class.getSimpleName());

4.設置各項配置信息

RCDHTextOption textOption = new RCDHTextOption.Builder()
                        .setTextColor(Color.RED)
                        .setGravity(Gravity.RIGHT)
                        .setTextSize(20)
                        .build();
                RoundCornerDialogHelperOption option = new RoundCornerDialogHelperOption.Builder()
                        .setCornerRadius(20)
                        .setBgColor(Color.RED)
                        .setTitleTextSize(24)
                        .setTitlePadding(30)
                        .setTitleTextAppearance(R.style.AppCompatTextView_BOLD_ITALIC)
                        .setTitleBgColor(Color.GRAY)
                        .setContentBgColor(Color.GREEN)
                        .setLeftButtonTextColor(Color.GREEN)
                        .setLeftButtonBgColor(Color.BLUE)
                        .setRightButtonTextOption(textOption)
                        .setRightButtonTextColor(Color.BLACK)
                        .setWidth(getResources().getDisplayMetrics().widthPixels)
                        .setHeight(getResources().getDisplayMetrics().heightPixels)
                        .setCancelable(false)
                        .setWindowAnimationStyleResId(R.style.Dialog_Animation)
                        .setOnRCDHClickLeftButtonListener(new OnRCDHClickLeftButtonListener() {
                            @Override
                            public void onClickLeftButton(RoundCornerDialogFragment fragment, TextView v) {
                                Toast.makeText(MainActivity.this, "左邊", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setOnRCDHClickRightButtonListener(new OnRCDHClickRightButtonListener() {
                            @Override
                            public void onClickRightButton(RoundCornerDialogFragment fragment, TextView v) {
                                Toast.makeText(MainActivity.this, "右邊", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .build();
                RoundCornerDialogFragment fragment = RoundCornerDialogFragment.newInstance(option);
                fragment.show(getSupportFragmentManager(), RoundCornerDialogFragment.class.getSimpleName());

4.總結

具體的項目框架可以到GitHub上查看,https://github.com/MingYueChunQiu/RoundCornerDialogHelper.git ,碼雲地址:https://gitee.com/MingYueChunQiu/RoundCornerDialogHelper.git ,如果有任何意見或建議,歡迎反饋,覺得可以的話,幫忙點個star,謝謝!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章