안녕하세요.
커스텀 Dialog를 만드는 예제는 많이 찾으실수 있구요.
기본 AlertDialog 예제도 많이 있지요.
안드로이드에서 기본 제공하는 ApiDemos 프로젝트에서도 보면, AlertDialogSamples.java 에 다양한 AlertDialog를 띄우는 예제가 있답니다.
그런데,
위 그림처럼 기본 AlertDialog 를 조금 응용한 dialog를 띄우고 싶을 때가 있을 것입니다.
하단의 체크박스 라인만 빼면, 완벽하게 기본 다이얼로그 인데요.
기본 AlertDialog 예제에는 없고, 전부 Custom으로 만들자니 참 귀찬고...
아! 이것을 어떻게 간단하게 만들 방법 없을까...
역시 구글님 찾아보니 있더군요.
방법은 의외로 간단하더군요.
1. 하단의 체크박스 영역만 layout을 만듭니다. 저는 notshowagain.xml 이라는 이름으로 만들었답니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckBox
android:text="@string/use_default"
android:id="@+id/skip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
별것은 없고, 그냥 CheckBox 에서 String 리소스에 값을 가져오는 layout입니다.
다이얼로드 다른부분에 대한 것은 전혀 없습니다.
2. 기본 AlertDialog 를 만들면서 위 layout을 추가해 줍니다.
// 위에서 만든 layout을 가져오는 부분
LayoutInflater adbInflater = LayoutInflater.from(MovieView.this);
View notshowagainLayout = adbInflater.inflate(R.layout.notshowagain, null);
// 체크박스의 값을 읽어 오기 위해서
final CheckBox dontShowAgain = (CheckBox)eulaLayout.findViewById(R.id.skip);
new AlertDialog.Builder(MovieView.this)
.setTitle(result.title)
.setView(notshowagainLayout)
.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (dontShowAgain.isChecked())
{
// 저장에 필요한 코드 추가
}
// 다이얼로그 아이템 선택시 할 작업을 추가
}
}).setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// Back 키등 취소 되었을 때 할일 추가
}
}).show();
위 AlertDialog 는 setItems 를 이용하여 list를 가진 dialog를 생성하는 것에 setView 로 위에서 만든 layout를 추가한 것이랍니다.
위와 같은 방법을 사용하시면 기본 다이얼로드 하단에 원하는 형태의 layout을 추가할수 있답니다.
감사합니다 ^^