Android修改原生AlertDialog按钮的颜色大小边距位置以及标题居中等

先来看个效果图:

Android修改原生AlertDialog按钮的颜色大小边距位置以及标题居中等_第1张图片

直接上代码:

 private void showTip() {
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setPositiveButton("确定", (dialog1, which) -> dialog1.dismiss());
        AlertDialog alertDialog = dialog.create();
        TextView title = new TextView(this);
        title.setText("提示");
        title.setPadding(10, 30, 10, 10);
        title.setGravity(Gravity.CENTER);
        title.setTextSize(18);
        title.setTextColor(Color.BLACK);
        alertDialog.setCustomTitle(title);
        alertDialog.setMessage("更多博客请联系博主,查看xiayiye5博客,地址:https://blog.csdn.net/xiayiye5");
        alertDialog.setCancelable(false);
        alertDialog.show();
        Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
        LinearLayout.LayoutParams cancelBtnPara = (LinearLayout.LayoutParams) button.getLayoutParams();
        //设置按钮的大小
        cancelBtnPara.height = LinearLayout.LayoutParams.WRAP_CONTENT;
        cancelBtnPara.width = LinearLayout.LayoutParams.MATCH_PARENT;
        //设置文字居中
        cancelBtnPara.gravity = Gravity.CENTER;
        //设置按钮左上右下的距离
        cancelBtnPara.setMargins(100, 20, 100, 20);
        button.setLayoutParams(cancelBtnPara);
        button.setBackground(ContextCompat.getDrawable(this, R.drawable.food_subsidy_bg));
        button.setTextColor(ContextCompat.getColor(this, R.color.white));
        button.setTextSize(16);
    }

感谢博主:原博主链接

你可能感兴趣的:(Android总结)