Skip to content

Commit

Permalink
标题属性增加padding
Browse files Browse the repository at this point in the history
  • Loading branch information
hupei committed Jul 21, 2020
1 parent 331139c commit 68b15ad
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
case 0:
new CircleDialog.Builder()
.setTitle("标题")
.setSubTitle("我是副标题")
.configText(new ConfigText() {
@Override
public void onConfig(TextParams params) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@ public TitleParams[] newArray(int size) {
* 标题
*/
public String text;
/**
* 内间距 [left, top, right, bottom]
*/
public int[] padding = CircleDimen.TITLE_PADDING;

/**
* 标题高度
*/
public int height = CircleDimen.TITLE_HEIGHT;
public int height = 0;
/**
* 标题字体大小
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ public class CircleDimen {
* 文本对话框的内间距
*/
public static int[] TEXT_PADDING = {50, 50, 50, 50};
/**
* 副标题文字内间距
*/
public static int[] TITLE_PADDING = {50, 50, 50, 50};
/**
* 副标题文字内间距
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
final class TitleView extends ScaleLinearLayout {
private CircleParams mParams;
private ImageView mTitleIcon;
private ScaleTextView tvTitle;
private ScaleTextView tvSubTitle;

Expand All @@ -29,7 +30,6 @@ public TitleView(Context context, CircleParams params) {
}

private void init(CircleParams params) {

setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
setOrientation(LinearLayout.VERTICAL);

Expand All @@ -39,68 +39,87 @@ private void init(CircleParams params) {


ScaleRelativeLayout titleLayout = new ScaleRelativeLayout(getContext());
titleLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT
, LayoutParams.WRAP_CONTENT));
titleLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

setTitleBg(titleLayout, params, titleParams.backgroundColor, dialogParams.backgroundColor, dialogParams.radius);

setTitleBg(titleLayout, params, titleParams.backgroundColor
, dialogParams.backgroundColor, dialogParams.radius);
//标题图标
ImageView ivTitleIcon = new ImageView(getContext());
createTitleIcon(titleParams, titleLayout);

//标题
createTitle(titleParams, subTitleParams, titleLayout);

//副标题
createSubTitle(dialogParams, subTitleParams);

OnCreateTitleListener createTitleListener = params.createTitleListener;
if (createTitleListener != null) {
createTitleListener.onCreateTitle(mTitleIcon, tvTitle, tvSubTitle);
}
}

private void createTitleIcon(TitleParams titleParams, ScaleRelativeLayout titleLayout) {
mTitleIcon = new ImageView(getContext());
RelativeLayout.LayoutParams layoutParamsTitleIcon = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParamsTitleIcon.addRule(RelativeLayout.LEFT_OF, android.R.id.title);
layoutParamsTitleIcon.addRule(RelativeLayout.CENTER_VERTICAL);
ivTitleIcon.setLayoutParams(layoutParamsTitleIcon);
mTitleIcon.setLayoutParams(layoutParamsTitleIcon);
if (titleParams.icon != 0) {
ivTitleIcon.setImageResource(titleParams.icon);
ivTitleIcon.setVisibility(VISIBLE);
mTitleIcon.setImageResource(titleParams.icon);
mTitleIcon.setVisibility(VISIBLE);
} else {
ivTitleIcon.setVisibility(GONE);
mTitleIcon.setVisibility(GONE);
}
titleLayout.addView(ivTitleIcon);
//标题
titleLayout.addView(mTitleIcon);
}

private void createTitle(TitleParams titleParams, SubTitleParams subTitleParams, ScaleRelativeLayout titleLayout) {
tvTitle = new ScaleTextView(getContext());
tvTitle.setId(android.R.id.title);
RelativeLayout.LayoutParams layoutParamsTitle = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParamsTitle.addRule(RelativeLayout.CENTER_HORIZONTAL);
tvTitle.setLayoutParams(layoutParamsTitle);
tvTitle.setGravity(titleParams.gravity);
tvTitle.setHeight(titleParams.height);
if (titleParams.height != 0) {
tvTitle.setHeight(titleParams.height);
}
tvTitle.setTextColor(titleParams.textColor);
tvTitle.setTextSize(titleParams.textSize);
tvTitle.setText(titleParams.text);
int[] padding = titleParams.padding;
if (padding != null) {
tvTitle.setAutoPadding(padding[0], padding[1], padding[2], subTitleParams == null ? 0 : padding[3]);
}
tvTitle.setTypeface(tvTitle.getTypeface(), titleParams.styleText);
titleLayout.addView(tvTitle);
addView(titleLayout);
}

//副标题

private void createSubTitle(DialogParams dialogParams, SubTitleParams subTitleParams) {
if (subTitleParams != null) {
tvSubTitle = new ScaleTextView(getContext());
setSubTitleBg(tvSubTitle, subTitleParams.backgroundColor, dialogParams.backgroundColor);
tvSubTitle.setGravity(subTitleParams.gravity);
if (subTitleParams.height != 0)
if (subTitleParams.height != 0) {
tvSubTitle.setHeight(subTitleParams.height);
}
tvSubTitle.setTextColor(subTitleParams.textColor);
tvSubTitle.setTextSize(subTitleParams.textSize);
tvSubTitle.setText(subTitleParams.text);
int[] padding = subTitleParams.padding;
if (padding != null)
tvSubTitle.setAutoPadding(padding[0], padding[1], padding[2], padding[3]);
int[] paddingSub = subTitleParams.padding;
if (paddingSub != null) {
tvSubTitle.setAutoPadding(paddingSub[0], paddingSub[1], paddingSub[2], paddingSub[3]);
}
tvSubTitle.setTypeface(tvSubTitle.getTypeface(), subTitleParams.styleText);
addView(tvSubTitle);
}
OnCreateTitleListener createTitleListener = params.createTitleListener;
if (createTitleListener != null) {
createTitleListener.onCreateTitle(ivTitleIcon, tvTitle, tvSubTitle);
}
}

private void setTitleBg(ScaleRelativeLayout tv, CircleParams params, int tbg, int dbg, int radius) {
//如果标题没有背景色,则使用默认色
int bg = tbg != 0 ? tbg : dbg;

//有内容则顶部圆角
if (params.textParams != null || params.itemsParams != null || params.progressParams != null
|| params.inputParams != null || params.bodyViewId != 0 || params.lottieParams != null) {
Expand All @@ -123,7 +142,6 @@ private void setTitleBg(ScaleRelativeLayout tv, CircleParams params, int tbg, in
private void setSubTitleBg(ScaleTextView tv, int tbg, int dbg) {
//如果标题没有背景色,则使用默认色
int bg = tbg != 0 ? tbg : dbg;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
tv.setBackground(new CircleDrawable(bg, 0));
} else {
Expand All @@ -132,14 +150,18 @@ private void setSubTitleBg(ScaleTextView tv, int tbg, int dbg) {
}

public void refreshTitle() {
if (mParams.titleParams == null) return;
if (mParams.titleParams == null) {
return;
}
post(new Runnable() {
@Override
public void run() {
if (tvTitle != null)
if (tvTitle != null) {
tvTitle.setText(mParams.titleParams.text);
if (tvSubTitle != null)
}
if (tvSubTitle != null) {
tvSubTitle.setText(mParams.subTitleParams.text);
}
}
});
}
Expand Down

0 comments on commit 68b15ad

Please sign in to comment.