2012-05-27 2 views
9

일부 튜토리얼을 살펴 보았는데 Android Doc에서는 인스턴스화 할 때 LayoutInflater에 직접 액세스하지 않는다고 말합니다. 구글 문서에서 예제 :LayoutInflater를 직접 호출하는 것과 그렇지 않은 것의 차이점은 무엇입니까?

LayoutInflater inflater = (LayoutInflater)context.getSystemService 
    (Context.LAYOUT_INFLATER_SERVICE); 

내가 겪은 튜토리얼이 하나입니다

LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 

그래서 난 정말 이해하지 못하는 것은 차이가 분명 다른 코드 이외의 무엇이다. 어떤 설명이라도 대단히 감사합니다. 나는 안드로이드 문서가 우리가 따라야한다고 가정하지만, 차이점이 있는지 확실하지 않습니다.

답변

17

당신은 LayoutInflator.from 방법과 같이 보이는 것을 알 수 있습니다 귀하의 질문에 똑같은 일을하십시오. 당신이 읽고있는 튜토리얼이 정확하게 무엇을 말하는지 모르지만 기능상의 차이는 없습니다. from 메서드를 사용하면 약간의 타이핑이 필요하기 때문에 좋습니다.

+0

하하! 오 고마워! 정말 도움이되었습니다. 나는 또 다른 질문을했을지 모르지만 당신은 그 질문에 답을했다. 매우 감사! – Andy

2
LayoutInflater inflater = (LayoutInflater)context.getSystemService 
    (Context.LAYOUT_INFLATER_SERVICE); 

당신은 당신은 내가 그 차이는 코드에 말을 LayoutInflater Class

에서 static 방법을 사용 System Manager

LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 

에서 LayoutInflater Service Provider를 받고있다이 또한 호출 스택하지만 결과를 작성하는 방법 동일합니다 - 당신은 LayoutInflater을 얻을 것입니다.

/** 
* Obtains the LayoutInflater from the given context. 
*/ 
public static LayoutInflater from(Context context) { 
    LayoutInflater LayoutInflater = 
      (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    if (LayoutInflater == null) { 
     throw new AssertionError("LayoutInflater not found."); 
    } 
    return LayoutInflater; 
} 

이 두 줄의 코드를 의미합니다 : 당신은 안드로이드 소스를 열면

this

감사에 대해

관련 문제