TextView title = new TextView(activity);
title.setText("自定义标题");
title.setPadding(dip2px(activity, 10), dip2px(activity, 15), dip2px(activity, 10), dip2px(activity, 10));
title.setGravity(Gravity.CENTER);
title.setTextColor(Color.BLACK);
title.setTypeface(Typeface.DEFAULT_BOLD);
title.setBackgroundColor(Color.WHITE);
title.setTextSize(23);
TextView msg = new TextView(activity);
msg.setText("自定义内容、自定义内容、自定义内容、自定义内容、 \n");
msg.setPadding(dip2px(activity, 10), dip2px(activity, 10), dip2px(activity, 6), dip2px(activity, 10));
msg.setGravity(Gravity.LEFT);
// msg.setBackground(new ColorDrawable(android.graphics.Color.TRANSPARENT));
msg.setBackground(new ColorDrawable(Color.WHITE));
msg.setTextColor(Color.GRAY);
msg.setTextSize(16);
AlertDialog dialog = new AlertDialog.Builder(activity, R.style.MyDialogStyle)
.setCustomTitle(title)
.setView(msg)
.setPositiveButton("确定按钮", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// requestPermission(activity, pers);
}
})
.setNegativeButton("取消按钮", null).create();
dialog.show();
ViewGroup.MarginLayoutParams mp1 = new ViewGroup.MarginLayoutParams(0,ViewGroup.LayoutParams.WRAP_CONTENT); // 设置item的宽高
mp1.setMargins(dip2px(activity, 5), dip2px(activity, 5), dip2px(activity, 5), dip2px(activity, 5));//分别是margin那四个属性 左、上、右、下边距
LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(mp1);
LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(mp1);
lp1.weight=1;//权重
lp2.weight=1;//权重
Button posBtn = (Button) dialog.getButton(DialogInterface.BUTTON_POSITIVE);
Button negBtn = (Button) dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
// posBtn.setBackgroundColor(Color.RED);
posBtn.setTextColor(Color.WHITE);
negBtn.setTextColor(Color.WHITE);
posBtn.setTextSize(15);
negBtn.setTextSize(15);
posBtn.setLayoutParams(lp1);
negBtn.setLayoutParams(lp2);
posBtn.setPadding(dip2px(activity, 5), dip2px(activity, 5), dip2px(activity, 5), dip2px(activity, 5));//内边距
negBtn.setPadding(dip2px(activity, 5), dip2px(activity, 5), dip2px(activity, 5), dip2px(activity, 5));
int strokeWidth = 5;//边框宽度
int roundRadius = dip2px(activity, 12);//圆角半径
GradientDrawable gd = new GradientDrawable();
GradientDrawable gd1 = new GradientDrawable();
gd.setColor(Color.parseColor("#FF4444"));//颜色
gd1.setColor(Color.parseColor("#FFA8A8A8"));
gd.setCornerRadius(roundRadius);//圆角
gd1.setCornerRadius(roundRadius);
posBtn.setBackground(gd);//设置确定按钮背景颜色 和按钮圆角度数
negBtn.setBackground(gd1);//设置取消按钮背景颜色 和按钮圆角度数
public static int dip2px(Context context, float dipValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
dialog的自定义样式
<style name="MyDialogStyle" parent="@android:style/Theme.Holo.Light.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
最终成果
Screenshot_20230106-142851.png
网友评论