2011-07-03 6 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) 스크롤되지 않습니다.

message.setMovementMethod(LinkMovementMethod.getInstance()); 

문제 : 레이아웃 스크롤되고, 뷰트에는 "관성"(.. 저를 호출하는 방법을 알고하지만 당신이 이해 생각하지 않습니다.)

해결 방법 3)를 사용하여이 없다 Scrollview. 내가 시도 갈 거 야,하지만 더 쉬운 해결책이 있습니다 믿을 수 없어

...

답변

85

당신은 꽤 쉽게 메시지의 텍스트 뷰에 접근 한 다음의 크기를 변경할 수 있습니다. 나는 더 큰 크기로 테스트했지만 원하는 크기를 사용할 수있었습니다. 이미 그랬던 것처럼 텍스트가 멋지게 스크롤됩니다. 뷰의 ID는 android.R.id.message이다 : 나는 텍스트 뷰가 null 여부가 될 수있는 위험이 있는지 모르겠어요하지만

AlertDialog dialog = new AlertDialog.Builder(this).setMessage("Hello world").show(); 
    TextView textView = (TextView) dialog.findViewById(android.R.id.message); 
    textView.setTextSize(40); 

이, 아마 청소기 솔루션입니다.

+0

감사합니다. –

+1

음, text textView 객체가 null 인 것으로 보입니다. – gaara87

+2

대화 상자를 만들 때 setMessage()를 호출해야합니다. 그렇지 않으면 첫 번째 위치에 메시지 텍스트 뷰가 없습니다. –

3

내 솔루션은 ... 스크롤 컨테이너를 만든 다음 XML 레이아웃에서와 마찬가지로 ScrollView 내부에 TextView를 추가해야합니다. 내 setTitleFontsetBodyFont 방법에 null를 확인할 것을 확인

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

나는에 AlertDialog의 제목 및 메시지 텍스트를 변경하려면 다음 코드를 사용하고

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

또한 woking! 많은 감사합니다. –

24

.... 버튼에

+0

이것은 제목보기를 얻는 데 효과적입니다 (감사합니다!), 일단 내가 그것을 얻을 수있는 두 가지 글꼴을 설정할 수 : 표준 글꼴 및 작은 글꼴 (11.0 값). –

+0

이것은 자산 폴더에서 경고 상자의 제목으로 글꼴을 설정하는 데 도움이되었습니다. 좋은 분! – jmacedo

+1

고마워요! 이것을 사용하여 다음과 같이 제목 글꼴 크기를 변경할 수있었습니다. int alertTitle = context.getResources(). getIdentifier ("alertTitle", "id", "android"); 제목보기 = dialog.findViewById (alertTitle); if (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

이것이 왜 상행음을 얻지 못하는지 잘 모르겠다. 그것은 OP의 직접적인 질문에 대한 답변이 아닐지도 모르지만 메시지를 크게 또는 작게 만들 자마자 버튼 텍스트를 더 크게 만들고 싶을 것이다/작습니다. 어쨌든 런타임에 완료해야하는 경우이 작업을 수행하기위한 멋지고 자연스러운 (자, 적어도 javascript-y) 방법 인 것 같습니다. 나는 이것을 허용 된 좋은 대답과 결합했습니다 (http : // stackoverflow .com/a/6563075/165164) 두 줄을 청취자 안에 넣고 getDimen 호출에 신경 쓰지 않고 크기를 설정하십시오. 광고 최종을 선언하는 것을 잊지 마십시오! –

관련 문제