Android 커스텀 다이얼로그 생성

프로그래밍/Android 2013. 8. 23. 12:11

Android 커스텀 다이얼로그 생성


커스텀 다이얼로그 생성

- xml 레이아웃 생성 custom_dialog.xml 로 저장

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/layout_root"

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:padding="10dp"

/>

<ImageView android:id="@+id/image"

android:layout_width="wrap_conotent"

android:layout_height="fill_parent"

android:layout_marginRight="10dp"

/>

<TextView android:id="@+id/text"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:textColor="#FFF"

/>

</LinearLayout>

Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);

dialog.setContentView( R.layout.custom_dialog); // custom_dialog.xml 로 저장된 layout 설정 파일을 view 설정한다.
dialog.setTitle( "Custom Dialog");

TextView text = ( TextView)dialog.findViewById(R.id.text); // 해당 설정 파일내에서 id 가 text 인 TextView 를 찾는다.
text.setText("Hello, this is a custom dialog");
ImageView image = ( ImageView)dialog.findViewbyId(R.id.image); // 해당 설정 파일내에서 id 가 image 인 ImageView 를 찾는다.
image.setImageResource(R.drawable.android);



AlertDialog 의 커스텀 다이얼로그 생성

AlertDialog.Builder builder;
AlertDialog alertDialog;

Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(LAYOUT_INFLATER);
View layout = inflater.inflate( R.layout.cutom_dialog, 
	(ViewGroup)findViewById( R.id.layout_root));
TextView text = (TextView)layout.findViewById(R.id.text);
text.setText("Hello, this is a cutom dialog");
ImageView image = (ImageView)layout.findViewById( R.id.image);
image.setImageResource( R.drawable.android);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();


: