[英]How can we customize the fonts of an alert dialog by using styles?
How can we customize the fonts of android alert dialog using styles 我们如何使用样式自定义android警报对话框的字体
I found many solution using
setTypeFace()
method.我发现了许多使用
setTypeFace()
方法的解决方案。 But I want to customize the entire application alert dialog using styles.但是我想使用样式来自定义整个应用程序警报对话框。
I would like to change the title, message, buttons fonts. 我想更改标题,消息,按钮的字体。
I was able to change the message font using the following codes. 我可以使用以下代码更改消息字体。
My style declaration for alert dialog 我的警报对话框样式声明
<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 code to display alert dialog 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();
Review below screen 在屏幕下方查看
Please help me to change the title and buttons fonts using styles and also I would like to customize the font color for the negative & positive buttons. 请帮助我使用样式更改标题和按钮的字体,并且我想自定义否定和肯定按钮的字体颜色。
Advance thanks for your time and help!! 提前感谢您的时间和帮助!!
First of all create a CustomDialog
class which will extend Dialog
class of Android. 首先创建一个
CustomDialog
类,它将扩展Android的Dialog
类。 Following is the code for the same - 以下是相同的代码-
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();
}
}
Now here whenever you want an instance of Dialog box you just have to create the instance of your CustomDialog
class and the xml here custom_popup_dialog_box will be having all the customization's like Font family, text size, color etc. You just need to set the property inside the xml or programmatically. 现在在这里,只要您想要Dialog的实例,就只需要创建
CustomDialog
类的实例即可,这里的xml中的custom_popup_dialog_box将具有所有自定义项,例如字体系列,文本大小,颜色等。您只需要在内部设置属性xml或以编程方式。 Hope you find the solution. 希望您找到解决方案。 Let me know in case of further information.
如果需要进一步的信息,请告诉我。 Thanks.
谢谢。
Java code to display alert dialog code replace this code 用于显示警报对话框代码的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();
layout save lay_alertdialog 布局保存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>
This is what you're looking for: https://stackoverflow.com/a/10741161/10126669 这就是您要寻找的东西: https : //stackoverflow.com/a/10741161/10126669
All you need to do is to put your font in assets and change 您需要做的就是将字体放入资产中并进行更改
SpannableStringBuilder SS = new SpannableStringBuilder("My message here");
SS.setSpan (new CustomTypefaceSpan("", font2), 0, 4,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
to match the size of your SpannableStringBuilder 匹配您的SpannableStringBuilder的大小
SS.setSpan (new CustomTypefaceSpan("", font2), 0, SS.length(),Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
and then add the SpannableStringBuilderto the dialog as 然后将SpannableStringBuilder添加到对话框中,如下所示:
.setMessage(SS)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.