Android 查看方法调用顺序
Android View methods call order
我正在 android 中创建自己的自定义 NumberPicker
,代码如下:
package fs.mohammad.fun;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
public class NumberPicker extends android.widget.NumberPicker {
private final static String LOG_TAG = "M3@NumberPicker";
public NumberPicker(Context context) {
super(context);
Log.d(LOG_TAG, "NumberPicker(context)");
}
public NumberPicker(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
Log.d(LOG_TAG, "NumberPicker(context, attributeSet)");
}
@Override
public void addView(View child) {
super.addView(child);
Log.d(LOG_TAG, "addView(child)");
}
@Override
public void addView(View child, int index) {
super.addView(child, index);
Log.d(LOG_TAG, "addView(child, index)");
}
@Override
public void addView(View child, int width, int height) {
super.addView(child, width, height);
Log.d(LOG_TAG, "addView(child), width, height");
}
@Override
public void addView(View child, ViewGroup.LayoutParams params) {
super.addView(child, params);
Log.d(LOG_TAG, "addView(child, params)");
}
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
super.addView(child, index, params);
Log.d(LOG_TAG, "addView(child, index, params)");
}
}
这是我的布局代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/root_view"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="fs.mohammad.fun.MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<fs.mohammad.fun.NumberPicker
android:id="@+id/number_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:descendantFocusability="blocksDescendants"
/>
</RelativeLayout>
我希望首先调用构造函数,但这是我的日志输出:
08-13 08:33:08.384 17759-17759/fs.mohammad.fun D/M3@NumberPicker: addView(child, index, params)
08-13 08:33:08.384 17759-17759/fs.mohammad.fun D/M3@NumberPicker: addView(child, params)
08-13 08:33:08.384 17759-17759/fs.mohammad.fun D/M3@NumberPicker: NumberPicker(context, attributeSet)
我的问题是为什么 addView
在 constructor
之前调用?
NumberPicker
在构造函数中膨胀其 children。
所以 NumberPicker#addView
调用 LayoutInflater 将 children 添加到 NumberPicker
.
我正在 android 中创建自己的自定义 NumberPicker
,代码如下:
package fs.mohammad.fun;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
public class NumberPicker extends android.widget.NumberPicker {
private final static String LOG_TAG = "M3@NumberPicker";
public NumberPicker(Context context) {
super(context);
Log.d(LOG_TAG, "NumberPicker(context)");
}
public NumberPicker(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
Log.d(LOG_TAG, "NumberPicker(context, attributeSet)");
}
@Override
public void addView(View child) {
super.addView(child);
Log.d(LOG_TAG, "addView(child)");
}
@Override
public void addView(View child, int index) {
super.addView(child, index);
Log.d(LOG_TAG, "addView(child, index)");
}
@Override
public void addView(View child, int width, int height) {
super.addView(child, width, height);
Log.d(LOG_TAG, "addView(child), width, height");
}
@Override
public void addView(View child, ViewGroup.LayoutParams params) {
super.addView(child, params);
Log.d(LOG_TAG, "addView(child, params)");
}
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
super.addView(child, index, params);
Log.d(LOG_TAG, "addView(child, index, params)");
}
}
这是我的布局代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/root_view"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="fs.mohammad.fun.MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<fs.mohammad.fun.NumberPicker
android:id="@+id/number_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:descendantFocusability="blocksDescendants"
/>
</RelativeLayout>
我希望首先调用构造函数,但这是我的日志输出:
08-13 08:33:08.384 17759-17759/fs.mohammad.fun D/M3@NumberPicker: addView(child, index, params)
08-13 08:33:08.384 17759-17759/fs.mohammad.fun D/M3@NumberPicker: addView(child, params)
08-13 08:33:08.384 17759-17759/fs.mohammad.fun D/M3@NumberPicker: NumberPicker(context, attributeSet)
我的问题是为什么 addView
在 constructor
之前调用?
NumberPicker
在构造函数中膨胀其 children。
所以 NumberPicker#addView
调用 LayoutInflater 将 children 添加到 NumberPicker
.