Android 将自定义字体设置为对话框

Android 将自定义字体设置为对话框,android,user-interface,dialog,android-appcompat,Android,User Interface,Dialog,Android Appcompat,我们正在尝试将对话框的标题设置为自定义字体,因此尝试使用以下内容创建主题: @style/Dialog.Title @style/Dialog.TextAppearance.Title @字体/自定义字体 我们正在创建对话框生成器,如下所示: AlertDialog.Builder(ContextThemeWrapper(context, R.style.DialogTheme), R.style.DialogTheme) 样式和文字外观完全被忽略。对于其他风格,它似乎是可行的 我们复制了

我们正在尝试将对话框的标题设置为自定义字体,因此尝试使用以下内容创建主题:


@style/Dialog.Title
@style/Dialog.TextAppearance.Title
@字体/自定义字体
我们正在创建对话框生成器,如下所示:

AlertDialog.Builder(ContextThemeWrapper(context, R.style.DialogTheme), R.style.DialogTheme)
样式和文字外观完全被忽略。对于其他风格,它似乎是可行的

我们复制了AppCompat使用的布局,并将其添加为自定义标题,从而使其正常工作:


基于此:

这里我们使用的不是
DialogTitle
,而是
TextView
。什么
DialogTitle
在测量时过载,如下所示:

final Layout=getLayout();
if(布局!=null){
final int lineCount=layout.getLineCount();
如果(行数>0){
final int-ellipsiscont=layout.getellipsiscont(lineCount-1);
如果(省略>0){
设置单行线(假);
设置最大线(2);
最终类型Darray a=getContext()。获取样式数据属性(null,
R.styleable.text外观,
android.R.attr.textAppearanceMedium,
android.R.style.textpearance(中位);
最终int textSize=a.getDimensionPixelSize(
R.styleable.TextAppearance\u android\u textSize,0);
如果(textSize!=0){
//textSize已以像素表示
setTextSize(TypedValue.COMPLEX\u UNIT\u PX,textSize);
}
a、 回收();
超级测量(宽度测量、高度测量);
}
}
}

我们认为这与设置文本大小有关,但我们不确定。我们有一个工作黑客(复制的布局)。但是,我们更希望知道如何使用xml样式和主题,而不必更改默认行为。

从我对最近发布的另一个问题的回答来看,这就是我如何能够以编程方式设置
AlertDialog
的所有属性的字体。希望对你有用

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上工作

另一种通过TextView以编程方式设置标题的方法,我以前是这样做的:

TextView alertTitle = findViewById(R.id.alertTextTemp);
if (alertTitle.getParent() != null) {
    ((ViewGroup) alertTitle.getParent()).removeView(alertTitle);
}
alertTitle.setText("Warning");
alertTitle.setVisibility(View.VISIBLE);
alertTitle.setTypeface(semibold);
然后,当您创建对话框时,可以使用以下内容设置自定义标题:

new AlertDialog.Builder(MainActivity.this).setCustomTitle(alertTitle)...
alertTitle是
activity\u main.xml
文件中的文本视图:

<TextView
    android:id="@+id/alertTextTemp"
    android:visibility="gone"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="22sp"
    android:paddingLeft="23dp"
    android:paddingTop="22dp"
    android:textColor="#000000" />


TextView上的这些属性我必须进行微调,使其尽可能接近默认的标题边距和大小,您可能需要验证它是否适合您。

最后,我们采用了设置自定义标题视图的方法。因为我们希望从主题推断字体,而不必通过编程来实现。
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);