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

自己定义一个view

 1 <?xml version="1.0" encoding="utf-8"?>
 2 
 3 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 4               android:orientation="vertical"
 5               android:layout_width="match_parent"
 6               android:layout_height="match_parent"
 7               android:background="@color/white">
 8 
 9     <TextView
10             android:layout_width="match_parent"
11             android:layout_height="wrap_content"
12             android:id="@+id/textViewTitle"
13             android:textColor="@color/black"
14             android:gravity="center" android:textSize="26dp"/>
15 
16     <TextView
17             android:layout_width="match_parent"
18             android:layout_height="wrap_content"
19             android:id="@+id/textViewAuthor"
20             android:layout_gravity="left" android:textColor="@android:color/darker_gray" android:textSize="16dp"/>
21 
22     <ImageView
23             android:layout_width="wrap_content"
24             android:layout_height="wrap_content"
25             android:id="@+id/imageView"
26             android:layout_gravity="center_horizontal"
27             android:scaleType="center"/>
28 
29     <TextView
30             android:layout_width="match_parent"
31             android:layout_height="wrap_content"
32             android:id="@+id/textViewContent"
33             android:layout_gravity="center_horizontal" android:textColor="@color/black" android:textSize="20dp"/>
34 
35     <LinearLayout
36             android:layout_width="fill_parent"
37             android:layout_height="2dp"
38             android:layout_gravity="center"
39             android:background="@color/black">
40     </LinearLayout>
41 
42     <TextView
43             android:layout_width="match_parent"
44             android:layout_height="wrap_content"
45             android:id="@+id/textViewOtherInfo"
46             android:layout_gravity="left" android:clickable="true" android:textColor="@android:color/darker_gray"
47             android:textSize="16dp"/>
48 </LinearLayout>

对应的类

 1 public class ContentItemView extends LinearLayout {
 2 
 3     private TextView title;
 4     private TextView author;
 5     private TextView content;
 6     private TextView otherInfo;
 7     private ImageView contentImage;
 8 
 9     private ContentInfo info;
10 
11     public ContentItemView(Context context) {
12         super(context);
13         init(context);
14     }
15 
16     private void init(Context context) {
17         LinearLayout convertView =
18         (LinearLayout) LayoutInflater.from(context).inflate(R.layout.contentitem, null);
19         title = (TextView) convertView.findViewById(R.id.textViewTitle);
20         author = (TextView) convertView.findViewById(R.id.textViewAuthor);
21         content = (TextView) convertView.findViewById(R.id.textViewContent);
22         otherInfo = (TextView) convertView.findViewById(R.id.textViewOtherInfo);
23         contentImage = (ImageView) convertView.findViewById(R.id.imageView);
24     }
25 }

添加到一个activity中

 1 <?xml version="1.0" encoding="utf-8"?>
 2 
 3 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 4               android:orientation="vertical"
 5               android:layout_width="match_parent"
 6               android:layout_height="match_parent"
 7               android:background="@color/white" android:paddingLeft="12dp" android:paddingTop="12dp"
 8               android:paddingRight="12dp">
 9 
10     <ScrollView
11             android:layout_width="match_parent"
12             android:layout_height="match_parent"
13             android:id="@+id/scrollView">
14 
15         <view android:layout_width="match_parent"
16               android:layout_height="match_parent"
17               class="com.HighFunStudio.shudu.ContentItemView" android:id="@+id/view"/>
18     </ScrollView>
19 </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)

添加一个构造函数

1     public ContentItemView(Context context, AttributeSet paramAttributeSet) {
2         super(context, paramAttributeSet);
3         init(context);
4     }

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

断点调试,看到调用栈

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

 

转载于:https://www.cnblogs.com/HighFun/p/3281701.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
大家好,今天给大家分享一下Android里的Context的一些用法. 这里大致可以分为两种:一是传递Context参数,二是调用全局的Context. 其实我们应用启动的时候会启动Application这个类,这个类是在AndroidManifest.xml文件里其实是默认的 为了让大家更容易理解,写了一个简单的Demo.步骤如下: 第1步:新建一个Android工程ApplicationDemo,目录结构如下: 第2步:新建一个工具类ToolsUtil.java,代码如下 package com.tutor.application; import android.content.Context; import android.widget.Toast; /** * @author carlshen. * 应用的一些工具类. */ public class ToolUtils { /** * 参数带Context. * @param context * @param msg */ public static void showToast(Context context,String msg){ Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); } /** * 调用全局的Context. * @param msg */ public static void showToast(String msg){ Toast.makeText(MainApplication.getContext(), msg, Toast.LENGTH_SHORT).show(); } } 第3步:新建一个View命名为MainView.java就是我们Activity现实的View.代码如下: package com.tutor.application; import android.app.Activity; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.FrameLayout; /** * @author carlshen. * 自定义的MainView. */ public class MainView extends FrameLayout implements View.OnClickListener{ private Context mContext; private Activity mActivity; /** * 参数Button. */ private Button mArgButton; /** * 全局Button. */ private Button mGlobleButton; /** * 退出Button. */ private Button mExitButton; public MainView(Context context){ super(context); setupViews(); } public MainView(Context context, AttributeSet attrs) { super(context, attrs); setupViews(); } private void setupViews(){ //获取View的上下文. mContext = getContext(); //这里将Context转换为Activity. mActivity = (Activity)mContext; LayoutInflater inflater = LayoutInflater.from(mContext); View v = inflater.inflate(R.layout.main, null); addView(v); mArgButton = (Button)v.findViewById(R.id.arg_button); mGlobleButton = (Button)v.findViewById(R.id.glo_button); mExitButton = (Button)v.findViewById(R.id.exit_button); mArgButton.setOnClickListener(this); mGlobleButton.setOnClickListener(this); mExitButton.setOnClickListener(this); } public void onClick(View v) { if(v == mArgButton){ ToolUtils.showToast(mContext, "我是通过传递Context参数显示的!"); }else if(v == mGlobleButton){ ToolUtils.showToast("我是通过全局Context显示的!"); }else{ mActivity.finish(); } } } 这里MainView.java使用的布局main.xml代码如下: <?xml version="1.0" encoding="utf-8"?> 第4步:修改ApplicationDemoActivity.java,代码如下: package com.tutor.application; import android.app.Activity; import android.os.Bundle; public class ApplicationDemoActivity extends Activity { private static Context aContext; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MainView mMainView = new MainView(this); setContentView(mMainView); aContext = getApplicationContext(); } /**获取Context. * @return */ public static Context getContext(){ return aContext; } } 第5步:运行上述工程效果如下:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值