我在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>
我希望首先调用构造函数,但这里是我的Log输出:
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
?
答案 0 :(得分:1)
NumberPicker
在构造函数中夸大了它的孩子。
因此,LayoutInflator调用NumberPicker#addView
将子项添加到NumberPicker
。