티스토리 뷰
Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.
Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.
아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.
How to change theme for AlertDialog
AlertDialog의 테마를 변경하는 방법
문제 내용
I was wondering if someone could help me out. I am trying to create a custom AlertDialog. In order to do this, I added the following line of code in styles.xml
누가 좀 도와줄 수 있을까 해서요. 커스텀 AlertDialog를 만들려고 합니다. 이를 위해 styles.xml에 다음 코드 행을 추가했습니다.
<resources>
<style name="CustomAlertDialog" parent="android:Theme.Dialog.Alert">
<item name="android:windowBackground">@drawable/color_panel_background</item>
</style>
</resources>
- color_panel_background.9.png is located in drawable folder. This is also available in Android SDK res folder.
color_panel_background.9.png은 drawable 폴더에 있습니다. Android SDK res 폴더에서도 사용할 수 있습니다.
The following is the main activity.
다음은 메인 액티비티입니다.
package com.customdialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
public class CustomDialog extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.setTheme(R.style.CustomAlertDialog);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("HELLO!");
builder .setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//dialog.cancel();
}
});
AlertDialog alertdialog = builder.create();
alertdialog.show();
}
}
In order to apply the theme to an AlertDialog, I had to set the theme to the current context.
AlertDialog에 테마를 적용하려면 현재 컨텍스트로 테마를 설정해야 했습니다.
However, I just can't seem to get the app to show customized AlertDialog. Can anyone help me out with this?
그러나 커스텀 AlertDialog를 앱에 표시할 수 없습니다. 누가 이것 좀 도와줄 수 있을까요?
높은 점수를 받은 Solution
In Dialog.java (Android src) a ContextThemeWrapper is used. So you could copy the idea and do something like:
Dialog.java(Android src)에서는 ContextTemeWrapper가 사용됩니다. 그래서 그 아이디어를 복사하여 다음과 같이 작업을 수행할 수 있습니다.
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDialogCustom));
And then style it like you want:
그런 다음 원하는 대로 스타일을 지정합니다.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AlertDialogCustom" parent="@android:style/Theme.Dialog">
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
<item name="android:textSize">10sp</item>
</style>
</resources>
가장 최근 달린 Solution
You can directly assign a theme when you initiate the Builder:
빌더를 시작할 때 테마를 직접 할당할 수 있습니다.
AlertDialog.Builder builder = new AlertDialog.Builder(
getActivity(), R.style.MyAlertDialogTheme);
Then customize your theme in your values/styles.xml
그런 다음 values/styles.xml에서 테마를 커스텀하세요.
<!-- Alert Dialog -->
<style name="MyAlertDialogTheme" parent="Theme.AppCompat.Dialog.Alert">
<item name="colorAccent">@color/colorAccent</item>
<item name="android:colorBackground">@color/alertDialogBackground</item>
<item name="android:windowBackground">@color/alertDialogBackground</item>
</style>
출처 : https://stackoverflow.com/questions/2422562/how-to-change-theme-for-alertdialog
'개발 > 안드로이드' 카테고리의 다른 글
액티비티 타이틀 바 숨기는 방법 (0) | 2022.12.16 |
---|---|
'Error type 3 Error: Activity class {} does not exist' 오류 수정하기 (0) | 2022.12.16 |
안드로이드에서 투명한 액티비티 만들기 (0) | 2022.12.14 |
inflate 할 때 발생한 'Avoid passing null as the view root' 오류 수정하기 (0) | 2022.12.14 |
동적(코드)으로 뷰에 dp단위로 마진 적용하기 (0) | 2022.12.13 |