我们如何使用样式自定义警告对话框的字体?

Anj*_*han 6 android android-alertdialog android-styles material-design

我们如何使用样式自定义android警报对话框的字体

我找到了很多使用setTypeFace()方法的解决方案。但我想使用样式自定义整个应用程序警报对话框。

我想更改标题、消息、按钮字体。

我能够使用以下代码更改消息字体。

我的警报对话框样式声明

<style name="MyAlertDialougeTheme" parent="@android:style/Theme.Material.Light.Dialog.Alert">
    <item name="android:textAppearanceSmall">@style/MyTextAppearance</item>
    <item name="android:textAppearanceLarge">@style/MyTextAppearance</item>
    <item name="android:textAppearanceMedium">@style/MyTextAppearance</item>

</style>
Run Code Online (Sandbox Code Playgroud)

显示警报对话框的 Java 代码

 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this,
                R.style.MyAlertDialougeTheme);
        builder.setTitle("Warning")
                .setMessage("My message here")
                .setPositiveButton("yes", null)
                .setNegativeButton("no", null)
                .show();
Run Code Online (Sandbox Code Playgroud)

在屏幕下方查看

在此处输入图片说明

请帮助我使用样式更改标题和按钮字体,我还想自定义负按钮和正按钮的字体颜色。

提前感谢您的时间和帮助!!

Sal*_*han 0

首先创建一个CustomDialog类来扩展DialogAndroid 的类。以下是相同的代码 -

public class CustomDialog extends Dialog implements
        View.OnClickListener {

    Activity context;
    private Button mBtnOK;

    public CustomDialog(Activity context) {
        super(context);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.custom_popup_dialog_box);
        mBtnOK = findViewById(R.id.btn_ok);
        mBtnOK.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_ok:
                dismiss();
                break;
            default:
                break;
        }
        dismiss();
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,每当您想要一个对话框实例时,您只需创建类的实例CustomDialog,这里的 xml custom_popup_dialog_box将具有所有自定义功能,例如字体系列、文本大小、颜色等。您只需在xml 或以编程方式。希望你找到解决方案。如果有更多信息,请告诉我。谢谢。