我想为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的权限,但是它不应用这种字体。
答案 0 :(得分:0)
您要实现以下截图吗?
如果是这样,首先您应该创建一个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
有我的演示。
答案 1 :(得分: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上运行。