0

단추를 클릭 할 때 조각을 확장하는 PlaceHodler 클래스의 팝업 창을 표시하려고합니다. 테스트를 위해 필자는이 코드를 작성했지만 실제로는 작동하지만 바보 같다고 생각합니다 (Button 객체를 부모보기로 사용하는 등 다른 방식으로 작동하지 않습니다 ...). 이 코드를보고 개선 방법을 알려주십시오. 내가 프로그래밍의 초보자이기 때문에 저를 판단하지 마십시오.조각에서 팝업 창 표시

내 코드 :

public static class PlaceholderFragment extends Fragment { 

    public PlaceholderFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
     final Button button1 = (Button)rootView.findViewById(R.id.button1); 
     button1.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Log.i("ilog", "onClick() works"); 
       PopupWindow pw = new PopupWindow(getActivity()); 
       TextView tv = new TextView(getActivity()); 
       LayoutParams linearparams1 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
       tv.setLayoutParams(linearparams1); 
       tv.setText("Testing"); 
       pw.setContentView(tv); 
       pw.setWidth(400); 
       pw.setHeight(180); 
       pw.showAtLocation(button1, Gravity.CENTER_HORIZONTAL, 25, 25); 
       pw.update(); 
      } 
     }); 
     return rootView; 
    } 
} 
+0

주목할 점은 버튼이 반드시 최종일 필요는 없다는 것입니다. onClick() 메서드에 전달되는 "보기"만 사용할 수 있습니다. 즉, OnClickListener를 할당 할 수있는 모든 작업을 수행 할 수 있습니다. 실험을 위해 TextView로 코드를 사용했습니다. 그러나 창문을 닫을 생각은 완벽하게 작동합니까? –

답변

0

당신이 할 수있는 것은이 작업을 수행하는 올바른 방법 중 하나입니다. 그러나이 방법은 활동이에, contrext을 얻을 제외하고

guide

+0

이것은 대화 상자이며 팝업 창에 대해 묻습니다.] – Salivan

2

조각 및 활동에 popwindow 거의 같은 권리 좋은 프로그래밍을 위해 그것을하기 위해이 가이드를 사용하여 조각 getActivity() 여기

입니다 이 코드는 생성하기 popWindow

View popupView = LayoutInflater.from(getActivity()).inflate(R.layout.popup_layout, null); 
    final PopupWindow popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT); 

// define your view here that found in popup_layout 
// for example let consider you have a button 

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

// finally show up your popwindow 
popupWindow.showAsDropDown(popupView, 0, 0); 

참조 PopupWindow