2017-11-08 15 views
-2

findViewById()을 수행하고 뷰 객체 또는 "보기 참조"를 반환 inflate() 객체에 객체 나에 refrence를 반환하고, 뷰의 부모 LinearLayout 뷰의 오브젝트가 linearLayouts 멤버 변수 및 메서드를 포함하는 방법.은?, 우리가</p> <pre><code>LinearLayout linear=(LinearLayout)inflate(R.layout.main,null); </code></pre> <p>그래서 돌아보기 개체 부풀려 경우 않기 때문에 요청

+1

Java 기본을 놓친 것처럼 보입니다. –

답변

0
// use this in activity 
LinearLayout ll=(LinearLayout)findViewById(R.id.llout); 

// use this in fragment onViewCreated() 
LinearLayout ll=(LinearLayout)View.findViewById(R.id.llout); 

// used anywhere in actvity or fragment 
LinearLayout ll=(LinearLayout)getView.findViewById(R.id.llout); 
+0

"view obj"자체 또는 "객체에 대한 유형보기의 참조" –

0

예 그들이 할, 당신은 LinearLayout 에 해당보기를 캐스팅 그리고 당신은 inflate

+0

"view obj"자체 또는 "객체에 대한 유형보기의 참조" –

+0

"객체보기"가 반환됩니까? Ctrl + 마우스 오른쪽 버튼을 클릭하면 해당 메소드가 정확히 수행되는지 확인할 수 있습니다. –

+0

이들 모두는 뷰 객체를 반환합니다. –

0

합니까 findViewById를을 (전화 LayoutInflater에서 개체가)와() 팽창 뷰 객체 또는 "뷰를 반환해야하기 때문에 참조 "?

View (또는 하위 클래스) 개체를 반환합니다.

반환 풍선보기의 경우 Object, view는 LinearLayout의 부모이며 how view의 객체에는 linearLayouts 멤버 변수 및 메서드가 들어 있습니다. 레이아웃에서보기

당신이보기를 팽창

, 모든이 팽창 할 것이다, 그러나 반환 된 개체는 레이아웃의 최상위 볼 수 있습니다. 많은보기 ( ViewGroup에서 파생되는 모든 항목)는 "하위"보기를 가질 수 있습니다. 반환 된 뷰의 자식에 액세스하려면 findViewById()을 호출하거나 getChildAt()으로 직접 액세스하십시오.

의 우리가이 레이아웃 있다고 가정 해 봅시다 :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/text1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Hello world"/> 

    <TextView 
     android:id="@+id/text2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Stack Overflow"/> 

</LinearLayout> 

우리는 다음과 같은 코드를 작성할 수 있습니다 :이 경우

LayoutInflater inflater = LayoutInflater.from(context); 
View view = inflater.inflate(R.layout.my_layout, null); 

view는 우리의 레이아웃에서 최상위보기 년대 LinearLayout 될 것입니다 . 자식 TextView을 얻으려면 다음과 같이 쓸 수 있습니다.

TextView helloWorld = view.findViewById(R.id.text1); 
TextView stackOverflow = ((LinearLayout) view).getChildAt(1); // index is 0-based