简体   繁体   English

如何将我的自定义视图添加到xml中的布局?

[英]How to add my custom View to layout in xml?

There is the following xml: 有以下xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.ulnda.calendarapplication.MyCalendarView
        android:id="@+id/calendarView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

MyCalendarView: MyCalendarView:

public class MyCalendarView extends CalendarView {

    public MyCalendarView(Context context) {
        super(context);
    }

    @Override
    public void setOnClickListener(OnClickListener listener) {
//      View v;
//      v.setOn

    }

    @Override
    public boolean performClick() {
        Log.e("event", "perform_click");
        return true;
    }
}

All is good, but when I'm trying to execute this application I'll have got the following message: 一切都很好,但是当我尝试执行这个应用程序时,我会收到以下消息:

10-19 12:59:30.037: E/AndroidRuntime(30968): FATAL EXCEPTION: main
10-19 12:59:30.037: E/AndroidRuntime(30968): android.view.InflateException: Binary XML file line #7: Error inflating class com.ulnda.calendarapplication.MyCalendarView

How can I fix it? 我该如何解决?

You need to implement some constructors for this to work. 您需要为此实现一些构造函数。 View has the following: View具有以下内容:

  • View(Context context) 查看(上下文上下文)
  • [View(Context context, AttributeSet attrs)](http://developer.android.com/reference/android/view/View.html#View(android.content.Context, android.util.AttributeSet)) [View(Context context,AttributeSet attrs)](http://developer.android.com/reference/android/view/View.html#View(android.content.Context,android.util.AttributeSet))
  • [View(Context context, AttributeSet attrs, int defStyle)](http://developer.android.com/reference/android/view/View.html#View(android.content.Context, android.util.AttributeSet, int)) [View(Context context,AttributeSet attrs,int defStyle)](http://developer.android.com/reference/android/view/View.html#View(android.content.Context,android.util.AttributeSet,int) )

The second and third are used for XML inflation. 第二个和第三个用于XML通胀。 You need to implement the constructor for your view class that corresponds to the second, and possibly the third. 您需要为视图类实现与第二个(可能是第三个)对应的构造函数。 That is, implement: 即,实施:

  • MyCalendar(Context context, AttributeSet attrs) MyCalendar(Context context,AttributeSet attrs)

And add the following if you need (probably not): 如果需要,可以添加以下内容(可能不需要):

  • MyCalendar(Context context, AttributeSet attrs, int defStyle) MyCalendar(Context context,AttributeSet attrs,int defStyle)

Try to add these constructors too 尝试添加这些构造函数

public MyCalendarView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public MyCalendarView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM