2013-07-02 1 views
0

에서 편집. 나는 다음과 같이이 작업을 수행 :나는 다음과 같은 내용으로 안드로이드 XML 파일이 레이아웃을 부풀려 코드

부풀려 XML :

LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View layout = inflater.inflate(R.layout.quitgame, null); 

이하여 ImageButton 편집하십시오 : 그러나

ImageView imgView = (ImageView)findViewById(R.id.ivQuit); 
ImageButton imgBtnYes = (ImageButton)findViewById(R.id.btnYes); 
ImageButton imgBtnNo = (ImageButton)findViewById(R.id.btnNo); 

RelativeLayout.LayoutParams relParams = (RelativeLayout.LayoutParams)imgBtnYes.getLayoutParams(); 
relParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
imgBtnYes.setLayoutParams(relParams); 

을만큼의 LayoutParams와 세 개의 코드 라인은 내가 함께 만든 PopupWindow 존재이기 때문에 팽창 된 뷰는 나타나지 않습니다. 이 줄에 오류가 숨겨져있는 것 같습니다.

RelativeLayout.LayoutParams relParams = (RelativeLayout.LayoutParams)imgBtnYes.getLayoutParams(); 

그러나 나는 그 이유를 알지 못합니다.

아무에게도이 코드 줄 때문에 PopupWindows가 표시되지 않는 이유를 누구에게 말해 줄 수 있습니까? PopupWindow 자체 때문이거나 LayoutInflater 때문입니까?

+3

이'ImageView imgView = (ImageView) findViewById (R.id.ivQuit)'는 ImageButton과 마찬가지로'ImageView imgView = (ImageView) layout.findViewById (R.id.ivQuit)'이어야합니다. – Raghunandan

+0

대단히 감사합니다! 이제 작동합니다. 그 코드의 라인에 오류가 숨겨져 있다고 잠시 생각조차하지 않았다 ... – Mister004

+0

당신은 똑같이 대답 할 수 있도록 대답으로 게시 할 것입니다. – Raghunandan

답변

0

루트를 null로 설정하여 레이아웃을 부 풀리면 inflate 메소드가 반환 한 레이아웃 변수가 팽창 된 레이아웃의 루트 (이 경우 RelativeLayout)를 유지하게됩니다. 내가 부풀려진 레이아웃을 액티비티의 콘텐츠에 첨부하는 코드를 보지 못했습니다 (게시 한 코드가 완전하지 않을 수 있습니다). 이 경우 findViewById 호출은 null을 반환해야합니다. 또한 내가 아는 한, 뷰가 이미 액티비티의 어딘가에있는 뷰에 첨부되지 않으면 LayoutParams가 치수가 설정되지 않았으므로 정확하지 않을 것입니다 (뷰에 호출 된 경우에도 마찬가지입니다) 그건 분명히 null이 아닙니다).

+0

답변 주셔서 감사합니다! 내 게시물에 대한 첫 번째 의견은 이미 내 문제를 해결했습니다. 그러나 또 다른 시간은 당신이 나를 도울 수있는 시간을 보냈기 때문입니다. – Mister004

1

는이

LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View layout = inflater.inflate(R.layout.quitgame, null);   

의 의견을 초기화보기 개체를 사용합니다.

ImageView imgView = (ImageView)layout.findViewById(R.id.ivQuit); 
ImageButton imgBtnYes = (ImageButton)layout.findViewById(R.id.btnYes); 
ImageButton imgBtnNo = (ImageButton)layout.findViewById(R.id.btnNo); 

활동에 설정된 현재보기 계층 구조의 findViewById 수 있습니다. 귀하의 경우에는 귀하의 레이아웃을 팽창시키고 있습니다. 그래서 inlfated 뷰 객체를 사용하여 뷰를 초기화하십시오.