2014-01-07 3 views
0

실제로 옵션 메뉴 항목을 클릭하고 내 옵션 메뉴가 화면 하단에있는 경우 (splitActionBarWhenNarrow) 팝업 창이 나타납니다 (사용자 정의 레이아웃). 일부 예외가 발생했습니다. 코드 : 이와 같이안드로이드 옵션 메뉴에서 팝업 창

switch (item.getItemId()) { 
    case R.id.redid: 

     Toast.makeText(MainActivity.this,"red color", Toast.LENGTH_SHORT).show(); 
     break; 
    case R.id.blueid: 
     Toast.makeText(MainActivity.this,"blue color", Toast.LENGTH_SHORT).show(); 
     break; 
    case R.id.greenid: 

    LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View popupview=inflater.inflate(R.layout.popuplayout,null); 
    PopupWindow popwindow=new PopupWindow(popupview,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
    popwindow.showAsDropDown(item.getActionView(), 100, 100); 
+0

PopupWindow를 greenid로 표시 하시겠습니까? 적색과 청색의 토스트 메시지를 볼 수 있습니까? –

+0

예 토스트도 잘 작동합니다. – skyshine

답변

1

보십시오

switch (item.getItemId()) { 
    case R.id.redid: 

     Toast.makeText(MainActivity.this,"red color", Toast.LENGTH_SHORT).show(); 
     break; 
    case R.id.blueid: 
     Toast.makeText(MainActivity.this,"blue color", Toast.LENGTH_SHORT).show(); 
     break; 
    case R.id.greenid: 
     initiatePopupWindow(); 
     break; 
    } 

및 외부에서 onCreate

()이 붙여 :

private PopupWindow pwindo; 

private void initiatePopupWindow() { 
try { 
// We need to get the instance of the LayoutInflater 
LayoutInflater inflater = (LayoutInflater) PopupActivity.this 
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View layout = inflater.inflate(R.layout.popup,(ViewGroup) 

findViewById(R.id.popup_element)); 
pwindo = new PopupWindow(layout, 350, 350, 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(); 
} 
} 

이 방법으로 문제가 해결 될 수 있다고 생각합니다.

+0

@Peter 제 대답이 유용하다면, 추가 에너지를주는 투표를하십시오. :) 감사. –

관련 문제