4

我正在使用Android Support Library的修订版26.0.1在我的应用程序中设置自定义字体。在我的应用程序的主题,我说:如何使用支持库在AlertDialog中设置自定义字体26

<item name="android:fontFamily">@font/my_font</item> 

它的工作就像一个魅力,转换文本在我的整个应用程序到我的自定义字体。除了我的对话 - 特别是他们的头衔,消息,还有他们的NumberPickers。在那些地方,字体没有更新。 (单选按钮和复选框起作用;“是”/“否”按钮也一样)

有没有什么我忘记添加到我的主题或样式中?或者,这仅仅是不被支持库支持?

更多一点细节:我使用AppCompatDialogFragment来实现我所有的对话框。在他们的onCreateDialog()方法中,我使用AlertDialog.Builder创建一个对话框,然后将其返回。

感谢您的帮助!

+1

我认为这是在支持库中的错误申请标题自定义视图。在Android 26上,如果我为主要主题同时指定了android:fontFamily和app:fontFamily,它将起作用。低于26的对话标题字体不会改变。我已使用支持库版本27.0.2进行了测试。 – devconsole

回答

1

谢谢大家谁回答,但不幸的是没有这些解决方案为我工作。我希望他们会为别人工作。

我已经断定这是支持库中的一个错误,希望Google能够修复。在此期间,我开发了这个哈克解决方法:

public static void applyCustomFontToDialog(Context context, Dialog dialog) { 
    Typeface font = ResourcesCompat.getFont(context, R.font.my_font); 
    if (font != null) { 
     TextView titleView = dialog.findViewById(android.support.v7.appcompat.R.id.alertTitle); 
     TextView messageView = dialog.findViewById(android.R.id.message); 
     if (titleView != null) titleView.setTypeface(font, Typeface.BOLD); 
     if (messageView != null) messageView.setTypeface(font); 
    } 
} 

此作品通过扫描对话框的视图树由支持库让他们的ID标题和消息的看法。如果支持库改变这些ID,这将不再起作用(这就是为什么它是hacky)。希望谷歌修复这个问题,我不再需要这样做了。

+0

什么的哈克解决方案:d –

-1

您可以使用自定义Alert Dialog并使用Typeface设置字体。看看下面的代码片段。

AlertDialog dg = new AlertDialog.Builder(this).setMessage("Your Message").show(); 
TextView tv = (TextView) dg.findViewById(android.R.id.message); // Your TextView of custom Alert Dialog 
Typeface fc=Typeface.createFromAsset(getAssets(),"fonts/FONT"); // This is your font file name. 
tv.setTypeface(fc); 
-1

您可以在对话框中膨胀的自定义布局是这样的:

final android.support.v7.app.AlertDialog.Builder alertDialogBuilder = new android.support.v7.app.AlertDialog.Builder(context, 
R.style.LimitAlertDialogStyle); 
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View alertLayout = layoutInflater.inflate(R.layout.date_layout, null); 
TextView tv= (TextView) alertLayout.findViewById(R.id.tv); 

Typeface fc=Typeface.createFromAsset(getAssets(),"fonts/FONT"); 
tv.setTypeface(fc); 
0

创建对话框生成器时,应使用一个ContextThemeWrapper。像这样

ContextThemeWrapper wrappedContext = new ContextThemeWrapper(context, R.style.mystyle); 
AlertDialog.Builder builder = new AlertDialog.Builder(wrappedContext); 

如果您仅支持SDK 11及以上,你可能需要使用

ContextThemeWrapper wrappedContext = new ContextThemeWrapper(context, R.style.mystyle); 
AlertDialog.Builder builder = new AlertDialog.Builder(wrappedContext, R.style.mystyle); 
1

我发现每次创建AlertDialog时都只需要对Java代码进行单行更改。

步骤1

创建自定义,含有正确的字体集一个TextView可重复使用的布局。把它叫做alert_dialog。XML:

<?xml version="1.0" encoding="utf-8"?> 
<TextView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    style="@style/SomeStyleWithDesiredFont" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="@dimen/spacing_2x" /> 

步骤2

创建一个可重复使用的辅助函数的地方,会夸大这种布局和文本设置为你想要的字符串

public static TextView createMessageView(String message, Context context) { 
    TextView messageView = (TextView) LayoutInflater.from(context).inflate(R.layout.alert_dialog, null, false); 
    messageView.setText(message); 
    return messageView; 
} 

步骤3

在代码中的每个AlertDialog.Builder链中,替换此行:

.setMessage(messageString) 

这一行:

.setView(createMessageView(messageString, context)) 

(请注意,同样的做法应该为标题TextView的工作。你可以在你的建设者调用setCustomTitle())

+0

谢谢!我认为这个解决方案不如我的哈克:) – yuval

相关问题