【问题标题】:How can we customize the fonts of an alert dialog by using styles?我们如何使用样式自定义警报对话框的字体?
【发布时间】:2019-03-15 00:48:24
【问题描述】:

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

我使用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>

显示警告对话框的 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();

在屏幕下方查看

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

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

【问题讨论】:

  • 您需要为此使用自定义对话框。一会儿分享代码。
  • 如果您认为这是最佳解决方案,您可以接受答案。快乐编码:)
  • @Anju Mohan 检查我的答案!

标签: android material-design android-alertdialog android-styles


【解决方案1】:

首先创建一个CustomDialog 类,它将扩展Android 的Dialog 类。以下是相同的代码 -

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();
    }
}

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

【讨论】:

    【解决方案2】:

    这就是您要查找的内容: https://stackoverflow.com/a/10741161/10126669

    所有你需要做的就是把你的字体放在资产中并改变

    SpannableStringBuilder SS = new SpannableStringBuilder("My message here");
    SS.setSpan (new CustomTypefaceSpan("", font2), 0, 4,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
    

    匹配 SpannableStringBuilder 的大小

    SS.setSpan (new CustomTypefaceSpan("", font2), 0, SS.length(),Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
    

    然后将 SpannableStringBuilder 添加到对话框中

    .setMessage(SS)
    

    【讨论】:

      【解决方案3】:

      显示警报对话框代码的 Java 代码替换此代码

      final Dialog dialog = new Dialog(activity);
                  dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                  dialog.setCancelable(false);
                  dialog.setContentView(R.layout.lay_alertdialog);  
      
      
           Objects.requireNonNull(dialog.getWindow()).setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
      
      
      // view initialize
      
      //sample 
          Button dialogButton = dialog.findViewById(R.id.btn_ok);
          dialogButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
      
                  dialog.dismiss();
      
      
              }
          });
      //
      
      
          dialog.show();
      

      布局保存lay_alertdialog

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="24dp"
          android:gravity="center"
          android:orientation="vertical">
      
      
          <TextView
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="8dp"
              android:gravity="start"
              android:padding="4dp"
              android:text="Warning"
              android:textSize="18sp"
              android:textStyle="bold" />
      
          <TextView
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="8dp"
              android:gravity="start"
              android:padding="4dp"
              android:text="your message"
              android:textSize="16sp"
              android:textStyle="bold" />
      
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:gravity="end"
              android:orientation="horizontal">
      
              <TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_gravity="end"
                  android:layout_margin="8dp"
                  android:text="NO"
                  android:textColor="#911907"
                  android:textSize="18sp" />
      
              <TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_gravity="end"
                  android:layout_margin="8dp"
                  android:text="YES"
                  android:textColor="#00a851
                  android:textSize="18sp" />
          </LinearLayout>
      
      </LinearLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-14
        • 1970-01-01
        相关资源
        最近更新 更多