android中自定义view构造函数ContentItemView(Context context, AttributeSet paramAttributeSet)的用处

时间:2020-12-09 19:24:40

自己定义一个view

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

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textViewTitle"
android:textColor="@color/black"
android:gravity="center" android:textSize="26dp"/> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textViewAuthor"
android:layout_gravity="left" android:textColor="@android:color/darker_gray" android:textSize="16dp"/> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_gravity="center_horizontal"
android:scaleType="center"/> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textViewContent"
android:layout_gravity="center_horizontal" android:textColor="@color/black" android:textSize="20dp"/> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_gravity="center"
android:background="@color/black">
</LinearLayout> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textViewOtherInfo"
android:layout_gravity="left" android:clickable="true" android:textColor="@android:color/darker_gray"
android:textSize="16dp"/>
</LinearLayout>

对应的类

 public class ContentItemView extends LinearLayout {

     private TextView title;
private TextView author;
private TextView content;
private TextView otherInfo;
private ImageView contentImage; private ContentInfo info; public ContentItemView(Context context) {
super(context);
init(context);
} private void init(Context context) {
LinearLayout convertView =
(LinearLayout) LayoutInflater.from(context).inflate(R.layout.contentitem, null);
title = (TextView) convertView.findViewById(R.id.textViewTitle);
author = (TextView) convertView.findViewById(R.id.textViewAuthor);
content = (TextView) convertView.findViewById(R.id.textViewContent);
otherInfo = (TextView) convertView.findViewById(R.id.textViewOtherInfo);
contentImage = (ImageView) convertView.findViewById(R.id.imageView);
}
}

添加到一个activity中

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

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white" android:paddingLeft="12dp" android:paddingTop="12dp"
android:paddingRight="12dp"> <ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollView"> <view android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.HighFunStudio.shudu.ContentItemView" android:id="@+id/view"/>
</ScrollView>
</LinearLayout>

运行,提示错误:

08-25 14:58:28.165: ERROR/AndroidRuntime(1342): FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.HighFunStudio.shudu/com.HighFunStudio.shudu.ArticleActivity}: android.view.InflateException: Binary XML file line #15: Error inflating class com.HighFunStudio.shudu.ContentItemView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)

添加一个构造函数

     public ContentItemView(Context context, AttributeSet paramAttributeSet) {
super(context, paramAttributeSet);
init(context);
}

哦了,一切正常。但是原因是什么?

断点调试,看到调用栈

void rInflate(XmlPullParser parser, View parent, final AttributeSet attrs,
boolean finishInflate) throws XmlPullParserException, IOException

中创建一个view,最后调用函数调用的是带属性的构造函数来创建一个view

具体的调用栈如下

<1> main@830013385120, prio=5, in group 'main', status: 'RUNNING'
at com.HighFunStudio.shudu.ContentItemView.<init>(ContentItemView.java:51)
at java.lang.reflect.Constructor.constructNative(Constructor.java:-1)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at android.view.LayoutInflater.createView(LayoutInflater.java:587)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
at android.app.Activity.setContentView(Activity.java:1881)
at com.HighFunStudio.shudu.ArticleActivity.onCreate(ArticleActivity.java:48)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Method.java:-1)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(NativeStart.java:-1)

所以只有创建

public ContentItemView(Context context, AttributeSet paramAttributeSet)

这样的构造函数才能够正确的使用自定义view