Android:我可以覆盖具有自己布局的第三方 class(库)吗?
Android: Can I override a third party class (library) that has its own layout?
我在 github 上找到了一个非常好的日历库,我想使用它。它有自己的布局。
https://github.com/prolificinteractive/material-calendarview
我想更改某些 UI 功能,但我不想从头开始编写自己的日历。有什么方法可以覆盖这个库吗?到目前为止我所尝试的是产生错误。
我试着像这样扩展这个 class:
public class MyCustomCalendar extends MaterialCalendarView {
public MyCustomCalendar(Context context) {
super(context);
}
}
然后我使用 MyCustomCalendar
class 作为 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" />
并且我在 MainActivity
的 Fragment
中扩充此布局,如下所示:
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]
我好像无法像上面那样制作自己的 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中的视图时,系统将使用第二个或第三个构造函数而不是第一个。
我在 github 上找到了一个非常好的日历库,我想使用它。它有自己的布局。
https://github.com/prolificinteractive/material-calendarview
我想更改某些 UI 功能,但我不想从头开始编写自己的日历。有什么方法可以覆盖这个库吗?到目前为止我所尝试的是产生错误。
我试着像这样扩展这个 class:
public class MyCustomCalendar extends MaterialCalendarView {
public MyCustomCalendar(Context context) {
super(context);
}
}
然后我使用 MyCustomCalendar
class 作为 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" />
并且我在 MainActivity
的 Fragment
中扩充此布局,如下所示:
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]
我好像无法像上面那样制作自己的 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中的视图时,系统将使用第二个或第三个构造函数而不是第一个。