2011-07-03 62 views
31

我試圖將一些懶文本放入AlertDialog中。 唯一的問題是真正太大的默認字體大小,所以我想讓它變小。將字體大小更改爲AlertDialog

以下是我嘗試的所有解決方法及其問題。

解決方法1)使用TextView和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的滾動。

message.setMovementMethod(LinkMovementMethod.getInstance()); 

問題:佈局滾動,弼不存在「慣性」(不知道怎麼稱呼的..但我想你懂的。)

解決方法3)使用滾動查看。

這就是我要去嘗試,但我不相信有沒有更簡單的解決方案...

回答

85

其實你可以得到訪問消息的TextView中很容易地,然後改變它的大小。我測試了一個更大的尺寸,但是你可以使用任何你想要的尺寸。文本會很好地滾動,因爲它已經做到了。視圖的ID是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的可能是空或不是。

+0

謝謝您的回答。 –

+1

好吧,文本textView對象看起來似乎爲空 – gaara87

+2

構建對話框時必須調用setMessage(),否則首先不會有消息textview。 –

3

這是我的解決方案...您需要創建滾動容器,然後在ScrollView中添加TextView,就像在XML佈局中一樣。

AlertDialog alertDialog = new AlertDialog.Builder(this).create(); 
String str = getString(R.string.upgrade_notes); 
final ScrollView s_view = new ScrollView(getApplicationContext()); 
final TextView t_view = new TextView(getApplicationContext()); 
t_view.setText(str); 
t_view.setTextSize(14);  
s_view.addView(t_view); 
alertDialog.setTitle("Upgrade notes!"); 
alertDialog.setView(s_view); 
+0

也正在w咽!非常感謝。 –

24

我使用下面的代碼更改AlertDialog標題和消息文本...

final int alertTitle = ctx.getResources().getIdentifier("alertTitle", "id", "android"); 
setTitleFont((TextView) dlg.findViewById(alertTitle)); 
setBodyFont((TextView) dlg.findViewById(android.R.id.message)); 

...確保我在setTitleFontsetBodyFont方法檢查null

+0

這工作獲得標題查看(謝謝!),但是一旦我找到了,我只能設置兩種字體:標準字體和小字體(值爲11.0)。 –

+0

這對我來說很有用,可以將資產文件夾中的字體設置爲警告框的標題。好的! – jmacedo

+1

非常感謝!使用這個我能夠改變標題字體大小,如下所示: int alertTitle = context.getResources()。getIdentifier(「alertTitle」,「id」,「android」); 查看標題= dialog.findViewById(alertTitle);如果(title!= null && title instanceof TextView){(TextView)title).setTextSize(14); } –

4

對於按鈕:

final AlertDialog ad = new AlertDialog.Builder(mainScreen) 
.setPositiveButton("OK", null) 
.setNegativeButton("Cancel", null).create(); 

ad.setOnShowListener(new DialogInterface.OnShowListener() { 
              @Override 
              public void onShow(DialogInterface dialog) { 
               int textSize = (int) Helper.getDimen(mainScreen, R.dimen.textSize12); 
               ad.getButton(Dialog.BUTTON_POSITIVE).setTextSize(textSize); 
               ad.getButton(Dialog.BUTTON_NEGATIVE).setTextSize(textSize); 
              } 


             }); 

ad.show(); 
+0

不知道爲什麼這沒有得到任何upvotes。它可能不是OP的直接問題的答案,但只要你使信息變得更大/更小,你可能會想要更大的按鈕文本/ smaller。這似乎是一個很好的,自然的(至少是javascript-y)的方式來完成任務,如果它必須在運行時完成的話 我將它與接受的良好答案(http:// stackoverflow .com/a/6563075/165164),並將這兩條線放在收聽者的旁邊,同時,也不用擔心getDimen的調用,只需設置一個大小。 不要忘記宣佈最終的廣告! –