Android 更改“警报”对话框的标题字体
我想更改警报对话框标题的字体Android 更改“警报”对话框的标题字体,android,fonts,android-alertdialog,Android,Fonts,Android Alertdialog,我想更改警报对话框标题的字体 有人能告诉我怎么做吗?您必须充气或自定义并创建样式并应用于AlertDialog LayoutInflater li = LayoutInflater.from(ctx); View view = li.inflate(R.layout.formatted_dialog, null); AlertDialog.Builder builder = new AlertDialog.Builder(ctx); builder.setTitle("Formatted");
有人能告诉我怎么做吗?您必须充气或自定义并创建样式并应用于AlertDialog
LayoutInflater li = LayoutInflater.from(ctx);
View view = li.inflate(R.layout.formatted_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle("Formatted");
builder.setView(view);
LayoutInflater li = LayoutInflater.from(this);
View view = li.inflate(R.layout.link, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Formatted");
builder.setView(view).create().show();
TextView text=(TextView) findViewById(R.id.text);
下面是如何充气布局并将其应用于AlertDialog
LayoutInflater li = LayoutInflater.from(ctx);
View view = li.inflate(R.layout.formatted_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle("Formatted");
builder.setView(view);
LayoutInflater li = LayoutInflater.from(this);
View view = li.inflate(R.layout.link, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Formatted");
builder.setView(view).create().show();
TextView text=(TextView) findViewById(R.id.text);
定义指定布局中所需的所有格式和样式
您可以使用膨胀视图访问版面中定义的特定文本视图,即
LayoutInflater li = LayoutInflater.from(ctx);
View view = li.inflate(R.layout.formatted_dialog, null);
TextView label=(TextView)view.findViewById(R.id.i_am_from_formatted_layout_lable);
保存为res/layout/link.xml的布局示例:
在您的onCreate()
中,或在您想调用AlertDialog的位置或任何时候调用AlertDialog
LayoutInflater li = LayoutInflater.from(ctx);
View view = li.inflate(R.layout.formatted_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle("Formatted");
builder.setView(view);
LayoutInflater li = LayoutInflater.from(this);
View view = li.inflate(R.layout.link, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Formatted");
builder.setView(view).create().show();
TextView text=(TextView) findViewById(R.id.text);
如果您是从其他方法调用,请将
此
替换为上下文对象。尝试设置对话框的主题。更改“字体”属性,如下所示:
<style name="CustomDialog" parent="android:Theme.Dialog">
<item name="android:typeface">serif</item>
</style>
Dialog dialog = new Dialog(ActivityName.this);
dialog.setContentView(R.layout.dialog_name);
dialog.setCancelable(true);
dialog.findViewById(R.id.text).setTypeface(Typeface.createFromAsset(getAssets(), "font.ttf"));
衬线
尽管这会更改对话框中所有文本视图的字体。
因此,请确保将文本视图设置为正确的字体您可以按照以下代码块更改
警报对话框
的外观:
AlertDialog dialog = new AlertDialog.Builder(this).setMessage("Message").show();
TextView textView = (TextView) dialog.findViewById(android.R.id.message);
textView.setTextSize(10);//to change font size
//to change font family
Typeface face = Typeface.createFromAsset(getAssets(),"font/fontFileName.ttf");
textView.setTypeface(face);
将字体文件放在资产文件夹中。在我的例子中,我创建了一个名为font的子目录
您还可以检查是否有可接受的答案。包括此布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello_World"
android:textColorLink="#FF00FF"
/>
</LinearLayout>
另一个和是这样做的:
<style name="CustomDialog" parent="android:Theme.Dialog">
<item name="android:typeface">serif</item>
</style>
Dialog dialog = new Dialog(ActivityName.this);
dialog.setContentView(R.layout.dialog_name);
dialog.setCancelable(true);
dialog.findViewById(R.id.text).setTypeface(Typeface.createFromAsset(getAssets(), "font.ttf"));
将font.ttf文件放在assets文件夹中,并像上面那样使用它,而不是设置alertdialog的文本,您应该从布局中设置自定义视图。在执行此操作之前,请修改视图的字体。 试试这个例子
TextView content = new TextView(this);
content.setText("on another font");
content.setTypeface(Typeface.SANS_SERIF);
//如果您使用的是xml生成的视图,请使用第一个示例
AlertDialog.Builder myalert = new AlertDialog.Builder(this);
myalert.setTitle("Your title");
myalert.setView(content);
myalert.setNeutralButton("Close dialog", null);
myalert.setCancelable(true);
myalert.show();
xml代码…替换xml中的字体
<TextView
android:id="@+id/yourid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="your content" />
我找到了一个简单的解决方案 起初,我设置我的警报框的标题
builder.setTitle("My Title");
所以我无法更改它的字体
那么对我有效的是
我创建了一个简单的文本视图:
TextView tv2;
并设置我想要的TextView的所有属性
然后我换了我的
builder.setTitle("My Title");
符合
builder.setCustomTitle(tv2);
现在,我可以通过更改tv2的属性来更改标题颜色、字体等。只需使用以下行获取对话框标题的标识符:
int dialogTitle = mCtnx.getResources().getIdentifier( "alertTitle", "id", "android" );
在android.support.v7.app.AlertDialog中使用此选项
LayoutInflater li = LayoutInflater.from(ctx);
View view = li.inflate(R.layout.formatted_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle("Formatted");
builder.setView(view);
LayoutInflater li = LayoutInflater.from(this);
View view = li.inflate(R.layout.link, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Formatted");
builder.setView(view).create().show();
TextView text=(TextView) findViewById(R.id.text);
TextView title=(TextView)alertDialog.findViewById(R.id.alertTitle) 无答案提供了更改
AlertDialog
标题字体的方法。以下是我所做的:
创建以下类:
public static class TypefaceSpan extends MetricAffectingSpan {
private final Typeface typeface;
public TypefaceSpan(Typeface typeface) {
this.typeface = typeface;
}
@Override public void updateDrawState(TextPaint tp) {
tp.setTypeface(typeface);
tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
@Override public void updateMeasureState(TextPaint p) {
p.setTypeface(typeface);
p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
}
添加以下实用程序方法:
/**
* <p>Return spannable string with applied typeface in certain style</p>
*
* http://stackoverflow.com/questions/8607707/how-to-set-a-custom-font-in-the-actionbar-title
*
* @param typeface
* The typeface to set to the {@link SpannableString}
* @param string
* the string to place in the span
* @return SpannableString that can be used in TextView.setText() method
*/
public static SpannableString typeface(Typeface typeface, CharSequence string) {
SpannableString s = new SpannableString(string);
s.setSpan(new TypefaceSpan(typeface), 0, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return s;
}
你可以加上这个
TextView textViewt = (TextView) alertDialog.findViewById(android.R.id.title);
textViewt.setTypeface(your font typeface);
排队后
alertDialog.show();
你也可以试试Spanable
SpannableString s = new SpannableString("My App");
s.setSpan(new TypefaceSpan(this, "font.otf"), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
s.setSpan(new RelativeSizeSpan(1.3f), 0,s.length(), 0);
builder.setTitle(s)
有一种简单的方法可以将新的自定义视图设置为对话框的标题。我们可以定义每个自定义视图(如TextView),并为其添加一些自定义属性,最后将其设置为对话框的标题,如下所示:
AlertDialog.Builder builder = new AlertDialog.Builder(OrderItemsActivity.this);
TextView title_of_dialog = new TextView(getApplicationContext());
title_of_dialog.setHeight(50);
title_of_dialog.setBackgroundColor(Color.RED);
title_of_dialog.setText("Custom title");
title_of_dialog.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
title_of_dialog.setTextColor(Color.WHITE);
title_of_dialog.setGravity(Gravity.CENTER);
builder.setCustomTitle(title_of_dialog);
builder.create().show();
在这里,我定义了一个动态TextView并为其设置了一些属性。最后,我使用“setCustomTitle()”函数将其设置为对话框的标题。使用支持库26中的此方法
ResourcesCompat.getFont(context, R.font.<YOUR_FONT>);
ResourcesCompat.getFont(context,R.font.);
有没有简单的方法来代替创建自定义对话框?没有。我想更改标题的字体。但是它会更改标题的字体吗?在文本视图中,您可以使用字体类添加字体。标题是AlertDialog中的一个单独视图。只有在您没有使用AlertDialog的标题,而是在自定义视图中创建了一个标题的情况下,这才有效。这就是你刚才想说的吗?我想提一下。否则,我看不出这是怎么回事。谢谢帕万的帮助。。但是我没有使用系统字体。。我正在使用另一种自定义字体。。那么我也可以用自定义字体做这个吗?@DhavalAdesara当然。。你可以用自定义字体代替衬线,但它显示的是错误。。那个错误:错误:不允许字符串类型(在值为“Myfont.ttf”的“android:typeface”处)。你能解释一下如何在样式中设置自定义字体吗?@Pavan我认为你是不对的,根据Commonware,自定义字体只能通过编程指定,这让我很困惑。。一个简单的问题。。我可以设置文本视图来代替标题吗?哪个部分容易混淆?使用这个有什么问题吗,山姆。。我很困惑,我们是否可以使用TextView来设置警报框的标题,而不是使用builder.setTitle(“标题”)。。!但是现在我使用了setCustomTitle()而不是setTitle()。。并将textview的对象作为参数传递。。现在我可以通过改变文本视图的字体来改变标题的字体。。我认为这是一个合适的方法,尽管我想建议一个更正:TextView title=(TextView)dialog.findViewById(android.R.id.title);title.setTypeface(Typeface.createFromAsset(getAssets(),“font.ttf”))@Gaurav Arora知道如何在底页对话框中更改字体吗?不回答OP的问题。是的。只是想补充一下。。。您可以这样做:(TextView)findViewById(dialogTitle),因为此alertTitle是从TextView扩展而来的……这对于获取
电子邮件首选项的标题非常有用!也适用于Unity Android插件库的本地Java代码。。。这也会更改对话框图标。。。因此,考虑它强制执行它…所以,按钮如何?我如何改变它们的颜色或按钮?@ EyLyyE眼是否知道如何在底层对话框中这样做?完美的解决方案。它工作得很好。谢谢你的代码Jared.!!!:)它是Jared的完美解决方案,但不适用于正/负按钮标签。有类似的解决方案吗?@par4301在DialogFragment#onStart()
中,您可以获取按钮的引用并设置字体。示例:((AlertDialog getDialog()).getButton(AlertDialog.BUTTON_正数)。setTypeface(tf)
在2020年无效。