0

我真的很擔心.findViewById()方法及其在自定義複合視圖創建中的使用。我不知道確切的地方,它保證它永遠不會返回null自定義複合視圖和.findViewById()

比方說,我有這樣的習俗複合視圖,它被添加到一些.xml這樣的:

<com.app.path.TwoButtonsView 
      android:id="@+id/ok_cancel_view" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"/> 

two_buttons_view.xml

<?xml version="1.0" encoding="utf-8"?> 
<merge> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        xmlns:tools="http://schemas.android.com/tools" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:orientation="horizontal"> 

     <TextView 
      android:id="@+id/first_button" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:clickable="true" 
      android:gravity="center" 
      android:textAllCaps="true"/> 

     <TextView 
      android:id="@+id/second_button" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:clickable="true" 
      android:gravity="center" 
      android:textAllCaps="true"/> 
    </LinearLayout> 
</merge> 

TwoButtonsView.java

public class TwoButtonsView extends LinearLayout { 

    // views 
    private TextView mFirstButtonView; 
    private TextView mSecondButtonView; 

    public TwoButtonsView(Context context) { 
     super(context); 
     init(context, null); 
    } 

    public TwoButtonsView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(context, attrs); 
    } 

    public TwoButtonsView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     init(context, attrs); 
    } 

    @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
    public TwoButtonsView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
     init(context, attrs); 
    } 

    private void init(Context context, AttributeSet attrs) { 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     inflater.inflate(R.layout.two_buttons_view, this); 

     // retrieve views 
     mFirstButtonView = (TextView) findViewById(R.id.first_button); // is there a chance it will return null? 
     mSecondButtonView = (TextView) findViewById(R.id.second_button); // is there a chance it will return null? 
    } 
} 

我的問題是:.findViewById(RESOURCE_ID)是否有機會返回null在撥打inflater.inflate(R.layout.two_buttons_view, this);之後,還是應該撥打我的init()方法撥打onFinishInflate()回撥?

回答

2

我的問題是:是否有任何機會,.findViewById(RESOURCE_ID) 將調用 inflater.inflate後返回null權(R.layout.two_buttons_view,這一點)

不,沒有不是,只要你正在尋找的視圖在佈局中,並且你明確地添加到視圖的層次結構中。最後,findViewById環路View[],填寫addView

小記約

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
inflater.inflate(R.layout.two_buttons_view, this); 

ViewGroup具有靜態方法inflate,所以你不需要檢索inflater調用getSystemService

+0

非常感謝您的回答,@Blackbelt! 請問您能解釋一下'onFinishInflate()'的用途嗎? –

+1

這是一個回調函數,當所有在佈局中聲明的孩子都被添加時,它會被調用。在你的情況下,你正在手動添加 – Blackbelt

+0

再次感謝您的答案:D。最後一個問題:你說我手動添加了它們,但是另一種方式是「不手動」添加它們?當你在xml中聲明這些時,我想不出任何=/ –

相關問題