设置AlertDialog按钮的自定义字体

问题描述 投票:0回答:2

我想为AlertDialog Builder的正/负按钮设置自定义字体。我正在使用Xamarin.Android。

我创建了这样的构建器:

var alert = new AlertDialog.Builder(mvxTopActivity.Activity)
                .SetCustomTitle(CreateTitle(title, mvxTopActivity))
                .SetView(CreateMessage(message, mvxTopActivity))
                .SetCancelable(false);

我添加了正面和负面按钮:

alert.SetPositiveButton(ok, (s, e) => { tcs.SetResult(okResult); });

alert.SetNegativeButton(cancel, (s, e) => { tcs.SetResult(cancelResult); });

我设法设置标题和消息的字体,但我无法为按钮设置自定义字体。

更新:我在创建模态后尝试添加样式“

 alert.Show();

            var mvxTopActivity = Mvx.Resolve<IMvxAndroidCurrentTopActivity>();
            var font = Typeface.CreateFromAsset(mvxTopActivity.Activity.ApplicationContext.Assets, "fonts/Effra_Md.ttf");

            var btnYes = alert.FindViewById<Button>(Android.Resource.Id.Button1);
            btnYes.SetTypeface(font, TypefaceStyle.BoldItalic);

            var btnNo = alert.FindViewById<Button>(Resource.Id.modal_button_cancel);
            btnNo.SetTypeface(font, TypefaceStyle.Normal);

我没有访问Button1但我可以访问modal_button_cancel / modal_button_ok但它不适用这样的字体。

c# xamarin xamarin.android alertdialog
2个回答
0
投票

你想像下面的截图一样实现吗?

enter image description here

如果是这样,首先你应该创建一个Customlayout.axml

 <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
 <RelativeLayout
    android:layout_width="250dp"
    android:layout_height="250dp"
    android:layout_centerInParent="true"
     >

    <TextView
        android:id="@+id/dialog_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:gravity="center"
        android:text="Alert"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/dialog_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/dialog_title"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:text="This is message"
        android:textSize="14sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/dialog_btn_cancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:text="cancle"
            android:textColor="#AAAAAA"
            android:textSize="14sp" />

        <Button
            android:id="@+id/dialog_btn_sure"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:text="Yes"
            android:textSize="14sp" />
    </LinearLayout>
</RelativeLayout>

</RelativeLayout >

然后,你可以创建一个alertDialog,有代码。

      //1.inflate the Customlayout
        View content = LayoutInflater.Inflate(Resource.Layout.Customlayout, null);
        //2. Getting the view elements
        TextView textView = (TextView)content.FindViewById(Resource.Id.dialog_content);
        TextView alertTitle = (TextView)content.FindViewById(Resource.Id.dialog_title);

        Button button1 = (Button)content.FindViewById(Resource.Id.dialog_btn_cancel);

        Button button2 = (Button)content.FindViewById(Resource.Id.dialog_btn_sure);


        //3. Setting font
        textView.SetTypeface(Typeface.Serif, TypefaceStyle.BoldItalic);
        alertTitle.SetTypeface(Typeface.Serif, TypefaceStyle.BoldItalic);
        button1.SetTypeface(Typeface.Serif, TypefaceStyle.BoldItalic);
        button2.SetTypeface(Typeface.Serif, TypefaceStyle.BoldItalic);

        //4.create a new alertDialog
        Android.App.AlertDialog alertDialog = new Android.App.AlertDialog.Builder(this).Create();

        //5. set the view
        alertDialog.SetView(content);

        //6. show the dialog
        alertDialog.Show(); // This should be called before looking up for elements

有我的演示。

https://github.com/851265601/CustomDialogDemo


0
投票

这是我用来设置AlertDialog's标题,消息和button1字体的代码。

Typeface semibold = ResourcesCompat.getFont(this, R.font.product_bold);
Typeface regular = ResourcesCompat.getFont(this, R.font.product_regular);
AlertDialog myDialog = new AlertDialog.Builder(this).setTitle("Your title")
        .setMessage("Your message.")
        .setPositiveButton("Your button", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //your code
            }
        }).show();
int titleText = getResources().getIdentifier("alertTitle", "id", "android");
((TextView) myDialog.getWindow().findViewById(titleText)).setTypeface(semibold);
TextView dialogMessage = myDialog.getWindow().findViewById(android.R.id.message);
Button dialogButton = myDialog.getWindow().findViewById(android.R.id.button1);
dialogMessage.setTypeface(regular);
dialogButton.setTypeface(semibold);

确认在我的Android 9.0上工作,不能声称它可以在旧的API上工作。

© www.soinside.com 2019 - 2024. All rights reserved.