반응형
|
안드로이드(Android) PopupWindow 위젯을 이용해 팝업창 만들기 |
|
환경 : Eclipse Mars, Android 4.2.2 |
이번 예제는 PopupWindow 위젯을 이용해서 팝업 창을 만들 때 레이아웃을 적용하는 방법입니다. 이렇게 레이아웃 적용이 가능하다는 팝업 창을 아주 다양하게 꾸밀 수 있다는 것입니다. 일반 Activity 에 xml 레이아웃을 적용하듯 팝업창에도 적용이 가능합니다.
▼ PopupWindow 객체를 이용해 팝업창을 생성하기 위해서는 LayoutInflater 로
레이아웃 xml 을 객체화 한후 PopupWindow 인수로 넘기면 됩니다. 팝업창 레이아웃을 객체화 하는 소스는 다음과 같습니다.
// LayoutInflater 객체와 시킴 LayoutInflater inflater = (LayoutInflater) PopUpWinndowActivity.this .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.screen_popup, (ViewGroup) findViewById(R.id.popup_element));
▼ 팝업창 레이아웃에 대한 전체 xml 입니다.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_element"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:orientation="vertical"
android:padding="10sp" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#3F0099"
android:paddingBottom="@dimen/abc_action_bar_icon_vertical_padding"
android:paddingTop="@dimen/abc_action_bar_icon_vertical_padding"
android:text="레이아웃이 적용된 팝업창"
android:textColor="#FFFFFF" />
<Button
android:id="@+id/btn_close_popup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="false"
android:gravity="center_vertical|center_horizontal"
android:text="Close" />
</RelativeLayout>
▼ LayoutInflater 이용해 팝업창레이아웃을 객체화 하였다면 PopupWindow 객체에 인수로 넘기면 됩니다. 그리고 팝업창의 크기를 넘기는데 메인화면의 스크린에서 일정크기를 뺏습니다. 팝업창 레이아웃에 등록되어있는 버튼에 대한 이벤트도 등록하였습니다.
pwindo = new PopupWindow(layout, mWidthPixels-100, mHeightPixels-500, true); pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0); btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup); btnClosePopup.setOnClickListener(cancel_button_click_listener);
▼ 팝업창닫기 버튼에 대한 이벤트 연결함수입니다.
팝업창을 닫는 함수 dismiss() 호출합니다.
private OnClickListener cancel_button_click_listener =
new OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
}
};
▼ 메인 activity 의 전체 레이아웃 xml 입니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#3F0099"
android:paddingBottom="@dimen/abc_action_bar_icon_vertical_padding"
android:paddingTop="@dimen/abc_action_bar_icon_vertical_padding"
android:text="PopupWindow 위젯을 이용한 팝업만들기"
android:textColor="#FFFFFF" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/abc_action_bar_icon_vertical_padding"
android:text="Button" />
</LinearLayout>
▼ 메인 activity 의 전체 소스 입니다.
import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
public class PopUpWinndowActivity extends Activity {
private Button btnClosePopup;
private Button btnCreatePopup;
private PopupWindow pwindo;
private int mWidthPixels, mHeightPixels;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pop_up_winndow);
WindowManager w = getWindowManager();
Display d = w.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
d.getMetrics(metrics);
// since SDK_INT = 1;
mWidthPixels = metrics.widthPixels;
mHeightPixels = metrics.heightPixels;
// 상태바와 메뉴바의 크기를 포함해서 재계산
if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17)
try {
mWidthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(d);
mHeightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(d);
} catch (Exception ignored) {
}
// 상태바와 메뉴바의 크기를 포함
if (Build.VERSION.SDK_INT >= 17)
try {
Point realSize = new Point();
Display.class.getMethod("getRealSize", Point.class).invoke(d, realSize);
mWidthPixels = realSize.x;
mHeightPixels = realSize.y;
} catch (Exception ignored) {
}
btnCreatePopup = (Button) findViewById(R.id.button1);
btnCreatePopup.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
initiatePopupWindow();
}
});
}
private void initiatePopupWindow() {
try {
// LayoutInflater 객체와 시킴
LayoutInflater inflater = (LayoutInflater) PopUpWinndowActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.screen_popup,
(ViewGroup) findViewById(R.id.popup_element));
pwindo = new PopupWindow(layout, mWidthPixels-100, mHeightPixels-500, true);
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(cancel_button_click_listener);
} catch (Exception e) {
e.printStackTrace();
}
}
private OnClickListener cancel_button_click_listener =
new OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
}
};
}
반응형
'안드로이드 개발' 카테고리의 다른 글
| 안드로이드(Android) 스마트폰 맥주소(Mac Address) 확인하는 방법 (1) | 2015.03.09 |
|---|---|
| 안드로이드(Android) GPS 로 현재 이동 속도 알아오기 (5) | 2015.03.05 |
| 안드로이드(Android) Thread 를 이용해 로딩페이지 만들기 (2) | 2015.02.23 |
| 안드로이드(Android) GPS LocationListener 이용해서 도시이름 가져오기 (0) | 2015.02.12 |
| 안드로이드(Android) 카메라, 갤러리 호출후 이미지 잘래내서 화면에 표시 (12) | 2015.02.05 |
| 안드로이드(Android) Google Map 버전2 에서 PolyLine 이용해 라인,마크 그리기 (0) | 2015.02.02 |
| 안드로이드(Android) Sdcard 미디어 파일 읽어 MP3 실행하기 (0) | 2015.01.29 |
| 안드로이드(Android) MediaPlayer 이용해 raw 폴더의 MP3 듣기 (0) | 2015.01.26 |
