마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

팝업창 나타나는 위치 질문입니다.

0 추천
다른 레이아웃에서 버튼을 클릭하면 팝업창이 나타나도록 구현중입니다. 
다음과 같이 클래스를 작성하였는데 버튼을 클릭하면 팝업창이 중앙으로 나타나지 않고 왼쪽 상탄에 위치를 하게 됩니다. 
이것을 앱 화면 중앙으로 나타나게 할 수 있는법좀 알려주세요. 
 
그리고 아래 팝업창 레이아웃에 확인 버튼과 앱을 닫는 버튼이 있는데 해당 버튼에 각각 이벤트를 주는 기능도 알려주시면 감사합니다. 
 
 
public class MainActivity extends Activity implements OnClickListener {
 
Button button_popup;
 
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
 
// 팝업 윈도우를 출력하기 위한 버튼
 
button_popup = (Button) findViewById(R.id.popup);
 
button_popup.setOnClickListener(this);
 
}
 
@Override
public void onClick(View view) {
 
switch (view.getId()) {
case R.id.popup:
/* 윈도우 출력 */
 
break;
}
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
 
popupWindow.showAsDropDown(popupView, 0, (int) popupView.getBaseline()); 
 
 
 
}
 
 
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
 
}
 
===========================================================================================
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/etc_popup_02"
    android:layout_marginTop="70dp"
    android:orientation="vertical" >
 
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="320dp" >
 
        <ImageButton
            android:id="@+id/exit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginRight="31dp"
            android:layout_marginTop="26dp"
            android:background="@drawable/btn_close_n" />
 
        <Button
            android:id="@+id/btnok"
            android:layout_width="300dp"
            android:layout_height="50dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="36dp"
            android:background="@drawable/btn_round_05_n"
            android:text="확인"
            android:textSize="15dip" />
 
        <ImageView
            android:id="@+id/clock"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_below="@+id/exit"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp"
            android:background="@drawable/icon_time" />
 
        <TextView
            android:id="@+id/txtreserve"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/clock"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp"
            android:text=""
            android:textSize="30dp"
            android:textAppearance="?android:attr/textAppearanceLarge" />
 
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/exit"
            android:layout_below="@+id/txtreserve"
            android:layout_marginTop="13dp"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="18dp" />
 
    </RelativeLayout>
 
</LinearLayout>
 
ekewpe (160 포인트) 님이 2014년 1월 21일 질문

2개의 답변

0 추천

showAsDropDown은 기준되는 anchor뷰 에서 bottom-left corner에 기본적으로 표시가 됩니다,

x와 y의 offsset을 적절히 조정 해 보세요~

노예의집 (23,370 포인트) 님이 2014년 1월 21일 답변
0 추천

앵커뷰를 기준으로 위치를 설정할때는 showAsDropDown(Anckor view)를 사용하고,

그렇지 않을 경우는 showAtLocation 를 사용하면 됩니다.

아래처럼하면 화면 가운데 위치합니다.

popupView .showAtLocation(linear,Gravity.CENTER,0,0);

이벤트를 주기 위해서는 하나만 알아두시면 됩니다.

View popupView = layoutInflater.inflate(R.layout.popup, null);

이 inflate 한 뷰내에서 위젯을 찾아서 이벤트를 주셔야 합니다

버튼이라고 예를 들면

Button mBtn = (Button)popupView.findViewById(R.id.btn);

mBtn.setOnClickListener(lister l)

이스트플랙 (2,510 포인트) 님이 2014년 1월 21일 답변
그러면 만약에 팝업창을 닫는 이벤트 처리를 한다고 하면
mBtn.setOnClickListener(lister l) 여기에 어떻게 입력해야하나요?
popupView.dismiss() 하시면 됩니다.
...