自己定义一个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

android中自定义view构造函数ContentItemView(Context context, AttributeSet paramAttributeSet)的用处的更多相关文章

  1. android开发:Android 中自定义View的应用

    大家好我们今天的教程是在Android 教程中自定义View 的学习,对于初学着来说,他们习惯了Android 传统的页面布局方式,如下代码: <?xml version="1.0&q ...

  2. Android中自定义View和自定义动画

    Android FrameWork 层给我们提供了很多界面组件,但是在实际的商业开发中这些组件往往并不能完全满足我们的需求,这时候我们就需要自定义我们自己的视图和动画. 我们要重写系统的View就必须 ...

  3. Android中自定义View的MeasureSpec使用

    有时,Android系统控件无法满足我们的需求,因此有必要自定义View.具体方法参见官方开发文档:http://developer.android.com/guide/topics/ui/custo ...

  4. Android 中的View与ViewGroup

    Android重点知识--View和ViewGroup与自定义控件 作者:丁明祥 邮箱:2780087178@qq.com 一.基础 ViewGroup 参考资料: Android 手把手教您自定义V ...

  5. Android中的this、Activity、Context等

    Android中的this.Activity.Context.Application等虽然有相似之处,但是不能乱用,每一个都有自己的特点.用的时候不能太随意了. 避免context相关的内存泄露,注意 ...

  6. Android的自定义View及View的绘制流程

    目标:实现Android中的自定义View,为理清楚Android中的View绘制流程“铺路”. 想法很简单:从一个简单例子着手开始编写自定义View,对ViewGroup.View类中与绘制View ...

  7. android中实现view可以滑动的六种方法续篇(二)

    承接上一篇,上一篇中讲解了实现滑动的第五种方法,如果你还没读过,可点击下面链接: http://www.cnblogs.com/fuly550871915/p/4985482.html 这篇文章现在来 ...

  8. Android之自定义View学习(一)

    Android之自定义View学习(一) Canvas常用方法: 图片来源 /** * Created by SiberiaDante on 2017/6/3. */ public class Bas ...

  9. Android读取自定义View属性

    Android读取自定义View属性 attrs.xml : <?xml version="1.0" encoding="utf-8"?> < ...

随机推荐

  1. Postgresql 正则表达式(转)

    原文:http://blog.csdn.net/wugewuge/article/details/7704996 postgresql支持POSIX 风格的正则表达式,在postgresql中使用正则 ...

  2. 很靠谱linux常用命令

    vim是打开vim编辑器,别的编辑器还有vi(功能没有vim 强大),nano,emacs等等,感觉还是vim最强大,其次是vi,别的就要差一些了. 我听我们老师说,用图形界面本身已经会被高手笑了,如 ...

  3. jvm启动

    首先使用 Java 命令启动JVM 其次进行JVM配置的装载——根据当前路径和系统的版本去寻找jvm.cfg文件,装载配置. 每种需要java虚拟机的软件,都会带一个jvm.cfg.然后jvm.cfg ...

  4. Mybatis怎么在mapper中用多个参数

    原文地址:https://github.com/mybatis/mybatis-3/wiki/FAQ How do I use multiple parameters in a mapper? Jav ...

  5. java猫和猫的名字

    这篇文章之所以叫猫和猫的名字,是因为是以猫为案例来讲的 主要的内容就是java构造函数和参数的传递 class Animal { public static String name; Animal(S ...

  6. Keras实践:模型可视化

    Keras实践:模型可视化 安装Graphviz 官方网址为:http://www.graphviz.org/.我使用的是mac系统,所以我分享一下我使用时遇到的坑. Mac安装时在终端中执行: br ...

  7. MyBatis—mybatis-config.xml模板

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration PUBLIC & ...

  8. js中var a={}什么意思

    创建一个变量a, 并给a赋值:{}是一个空的对象,是 new Object();的简写.

  9. 2018年浙江中医药大学程序设计竞赛 Solution

    Problem A. Jhadgre的C语言程序 签. #include <bits/stdc++.h> using namespace std; int main() { puts(&q ...

  10. hdu5141 线段树

    这题说的是给了一串然后计算出这个串的最长递增子序列的长度,然后计算出有过少个子串[i,j] 他们的最长递增子序列和这整个子串的最长递增子序列相同,我们对于每个j最长递增子序列找出他在序列中的使成为最长 ...