gpt4 book ai didi

java - 更改 AlertDialog 中的字体

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:10:02 25 4
gpt4 key购买 nike

有人可以建议一种在动态创建的 AlertDialog(标题和正文中)中更改字体的方法吗?我尝试了很多方法,但都没有用。代码是:

public void onClick(View v) {

new AlertDialog.Builder( c )
.setTitle( data.eqselect )
// .set
.setIcon(R.drawable.icon)
.setMessage(Threads.myData[0] )
.setNegativeButton( "Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.d( "AlertDialog", "Negative" );
}
} )
.show();
}

最佳答案

您应该从布局中设置自定义 View ,而不是设置警报对话框的文本。在这样做之前,请修改 View 的字体。

TextView mycontent = (TextView) findViewById(R.id.yourid);
mycontent.setTypeface(Typeface.createFromAsset(getAssets(), "font.ttf")

要设置警报对话框的 View ,请调用 .setView(mycontent) 而不是 setMessage()尽管据我所知这不会改变您的头衔。

更新我发现你无法理解我在说什么,所以这里有一个完整的例子

TextView content = new TextView(this);
content.setText("on another font");
content.setTypeface(Typeface.SANS_SERIF);

//Use the first example, if your using a xml generated view

AlertDialog.Builder myalert = new AlertDialog.Builder(this);
myalert.setTitle("Your title");
myalert.setView(content);
myalert.setNeutralButton("Close dialog", null);
myalert.setCancelable(true);
myalert.show();

这是使用我们刚刚创建的 TextView,它没有边距等。如果您使用一个易于创建的布局文件,您将能够自定义所有这些设置,尽管您可以在代码中为此示例执行此操作。

当然,把字体换成你想要的..来自 xml 的示例 TextView 可以是:

<TextView
android:id="@+id/yourid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="your content" />

关于java - 更改 AlertDialog 中的字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10538482/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com