커스텀 대화상자(custom dialog)
커스텀 대화상자(custom dialog)
- 그냥 원하는대로 만들수 있고, 만들기도 쉬워서 자주 사용한다.
- 하나 짜놓고 그냥 다음에 쓸때 복붙하고 사용하면 되니 참 쉽다.
① 메인에 대충 버튼 하나 놓는다(activity_main.xml)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="30dp"
android:text="대화상자 열어 줘"
android:textColor="#000000"
android:textSize="30sp"/>
</LinearLayout>
② Dialog 레이아웃도 대충 만들어본다(dialog.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="이것은 대화상자"
android:textColor="#000000"
android:textSize="20sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="확인" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="취소" />
</LinearLayout>
</LinearLayout>
③ 버튼 누르면 대화상자 뜨게 코드 작성(MainActivity.java)
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
ShowDialog();
}
});
}
private void ShowDialog()
{
LayoutInflater dialog = LayoutInflater.from(this);
final View dialogLayout = dialog.inflate(R.layout.dialog, null);
final Dialog myDialog = new Dialog(this);
myDialog.setTitle("대화상자 제목이다");
myDialog.setContentView(dialogLayout);
myDialog.show();
Button btn_ok = (Button)dialogLayout.findViewById(R.id.btn_ok);
Button btn_cancel = (Button)dialogLayout.findViewById(R.id.btn_cancel);
btn_ok.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//OK 누르면 할거
}
});
btn_cancel.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
myDialog.cancel();
}
});
}
}
- 허접해보이지만 잘 꾸미면 이쁘게 나온다. 모서리같은데 둥글게 하고 제목도 setTitle말고 xml로 직접 꾸며주고
이미지도 넣고 배경색도 바꾸고 하면..
○ 나는 별거 없는 화면에는 웬만하면 전체화면으로 설정해서 dialog를 쓴다.
○ 위에서 좀만 바꾸면 전체화면으로 쓸 수 있다
private void ShowDialog()
{
LayoutInflater dialog = LayoutInflater.from(this);
final View dialogLayout = dialog.inflate(R.layout.dialog, null);
final Dialog myDialog = new Dialog(this,android.R.style.Theme_NoTitleBar_Fullscreen);
myDialog.setContentView(dialogLayout);
myDialog.show();
○ 전체화면은 아니라더라도 원하는 화면크기로 강제로 바꿔도 된다(MATCH_ 부분을 알아서 바꾸도록)
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
Window window = myDialog.getWindow();
layoutParams.copyFrom(window.getAttributes());
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(layoutParams);
'안드로이드(android) > 대화상자(Dialog)' 카테고리의 다른 글
Custom Dialog Class 분리해서 사용하기 (0) | 2016.11.09 |
---|---|
Dialog Dim 제거하기 (2) | 2016.11.08 |
Dialog Cancel, Back버튼 관련 (0) | 2016.07.26 |