自定义复合视图和 .findViewById()

Custom compound view and .findViewById()

我真的很担心 .findViewById() 方法及其在自定义复合视图创建中的使用。我不确定它保证永远不会的确切位置 return null.

假设我有这个自定义复合视图,并且它被添加到一些 .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) 是否有可能在调用 inflater.inflate(R.layout.two_buttons_view, this); 后立即 return null,或者我应该调用我的 init() 方法关于 onFinishInflate() 回调?

My question is: is there any chance that .findViewById(RESOURCE_ID) will return null right after calling inflater.inflate(R.layout.two_buttons_view, this)

不,没有,只要您要查找的视图在布局中并且您明确添加到视图的层次结构中即可。最后,findViewByIdView[] 上循环,由 addView 填充。

关于

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

ViewGroup 有静态方法 inflate,所以你不需要检索 inflater 调用 getSystemService