问题描述
我正在尝试将一些 loooong 文本放入 AlertDialog 中.唯一的问题是默认字体太大了,所以我想把它变小.
I am trying to put some loooong text into an AlertDialog.The only issue the default font size that is really too big, so I want to make it smaller.
以下是我尝试过的所有解决方法及其问题.
Here are all the workaround I tried and their issues.
解决方法 1) 使用 TextView 和 myView.setTextSize(12);
Workaround 1) Using a TextView and myView.setTextSize(12);
final TextView myView = new TextView(getApplicationContext());
myView.setText(myLongText);
myView.setTextSize(12);
final AlertDialog d = new AlertDialog.Builder(context)
.setPositiveButton(android.R.string.ok, null)
.setTitle(myTitle)
.setView(myView)
.create();
问题:布局不滚动
解决方法 2) 使 TextView 可滚动.
Workaround 2) making TextView scrollable.
message.setMovementMethod(LinkMovementMethod.getInstance());
问题:布局是滚动的,但没有惯性"(不知道如何称呼它......但我想你明白.)
Issues: Layout is scrolling, bute there is no "inertia" (don't know how to call that.. But I guess you understand.)
解决方法 3) 使用滚动视图.
这就是我要尝试的,但我不敢相信没有更简单的解决方案......
That's what I am going to try, but I cannot believe there are no easier solutions...
推荐答案
实际上,您可以非常轻松地访问消息的 TextView
,然后更改其大小.我用更大的尺寸进行了测试,但你可以使用任何你想要的尺寸.文本将很好地滚动,因为它已经这样做了.视图的 id
是 android.R.id.message
You can actually get access to the message's TextView
pretty easily, and then change it's size. I tested with a bigger size, but you could use whatever size you want. The text will scroll nicely as it already does. The view's id
is android.R.id.message
AlertDialog dialog = new AlertDialog.Builder(this).setMessage("Hello world").show();
TextView textView = (TextView) dialog.findViewById(android.R.id.message);
textView.setTextSize(40);
这可能是一个更简洁的解决方案,但我不确定 TextView
是否存在 null
的风险.
This is probably a cleaner solution, though I'm not sure if there's a risk that the TextView
could be null
or not.
这篇关于将字体大小更改为 AlertDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!