Java Android:我可以覆盖有自己布局的第三方类(库)吗?

Java Android:我可以覆盖有自己布局的第三方类(库)吗?,java,android,inheritance,android-library,android-inflate,Java,Android,Inheritance,Android Library,Android Inflate,我在github上找到了一个非常好的日历库,我想使用它。它有自己的布局 我想更改某些UI功能,但我不想从头开始编写自己的日历。有没有办法覆盖这个库?到目前为止,我所尝试的是产生一个错误 我试着这样扩展这个类: public class MyCustomCalendar extends MaterialCalendarView { public MyCustomCalendar(Context context) { super(context); } } 然后

我在github上找到了一个非常好的日历库,我想使用它。它有自己的布局

我想更改某些UI功能,但我不想从头开始编写自己的日历。有没有办法覆盖这个库?到目前为止,我所尝试的是产生一个错误

我试着这样扩展这个类:

public class MyCustomCalendar extends MaterialCalendarView {

    public MyCustomCalendar(Context context) {
        super(context);
    }
}
然后我使用
MyCustomCalendar
类作为
ConstraintLayout
中的视图,如下所示:

<com.example.test.MyCustomCalendar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
public class CalFragment  extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      super.onCreateView(inflater, container, savedInstanceState);
      return inflater.inflate(R.layout.fragment_calendar, container, false);
    }

}
但是,这会产生一个错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.test, PID: 19357
    android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class com.example.test.MyCustomCalendar
    Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class com.example.test.MyCustomCalendar
    Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
E/AndroidRuntime:致命异常:主
进程:com.example.test,PID:19357
android.view.InflateException:二进制XML文件行#8:二进制XML文件行#8:膨胀类com.example.test.MyCustomCalendar时出错
原因:android.view.InflateException:二进制XML文件行#8:错误膨胀类com.example.test.MyCustomCalendar
原因:java.lang.NoSuchMethodException:[类android.content.Context,接口android.util.AttributeSet]

看起来我无法像上面那样制作自己的XML布局,这是为什么?

您的问题可能与库无关。您需要为自定义视图实现以下构造函数:

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

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

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

在xml中使用视图时,系统将使用第二个或第三个构造函数,而不是第一个构造函数。

您的问题可能与库无关。您需要为自定义视图实现以下构造函数:

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

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

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

当您在xml中使用视图时,系统将使用第二个或第三个构造函数而不是第一个构造函数。

那么,您可以在本地克隆此库并对其进行更改?您是否错过了文档的这一部分?文档的哪一部分?您可以在本地克隆此库并对其进行更改?您是否错过了文档的这一部分?文件的哪一部分?这解决了错误你是对的。但是它会使用哪一个呢?或者我在构造函数中编写的任何逻辑都应该包含在它们中?在这种情况下,拥有第二个构造函数就足够了。你可以在这里读到更多:这确实解决了错误,你是对的。但是它会使用哪一个呢?或者我在构造函数中编写的任何逻辑都应该包含在它们中?在这种情况下,拥有第二个构造函数就足够了。您可以在此处阅读更多内容: