페이지

2011년 5월 23일 월요일

액티비티를 다이얼로그로 띄우기 - CustomDialogActivity

Dialog 테마를 사용해서 Activity 를 Dialog 처럼 배경위에 띄울 수 있다.

다른 Activity 에서 아래 CustomDialogActivity 를 Intent 로 호출하면 Dialog 창이 뜬다.


[ CustomDialogActivity.java ]

public class CustomDialogActivity extends Activity {
    @Override
 protected void onCreate(Bundle savedInstanceState) {
        // Be sure to call the super class.
        super.onCreate(savedInstanceState);
       
        // See assets/res/any/layout/dialog_activity.xml for this
        // view layout definition, which is being set here as
        // the content of our screen.
        setContentView(R.layout.custom_dialog_activity);
    }
}

[ custom_dialog_activity.xml ]

<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:gravity="center_vertical|center_horizontal"
    android:text="@string/custom_dialog_activity_text"/>

위의 두 파일을 보면 Activity 에서 TextView 를 보여주는것 외에는 별다른점이 없다.

어디에서 CustomDialog 를 띄워 주는 것일까?

이번 예제에서 가장 중요한 핵심은 AndroidManifest.xml 파일에 있다.

AndroidManifest.xml  파일에서 CustomDialogActivity 를 정의한 부분을 자세히 보자.

[ AndroidManifest.xml ]

        <activity android:name=".app.CustomDialogActivity"
                android:label="@string/activity_custom_dialog"
                android:theme="@style/Theme.CustomDialog">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.SAMPLE_CODE" />
            </intent-filter>
        </activity>

activity 에 테마를 적용하는것을 볼수있다.

[ style.xml ]

    <style name="Theme.CustomDialog" parent="android:style/Theme.Dialog">
        <item name="android:windowBackground">@drawable/filled_box</item>
    </style>


[ filled_box.xml ]

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#f0600000"/>
    <stroke android:width="3dp" color="#ffff8080"/>
    <corners android:radius="3dp" />
    <padding android:left="10dp" android:top="10dp"
        android:right="10dp" android:bottom="10dp" />
</shape>

댓글 없음:

댓글 쓰기