我正在使用Java在HarmonyOS中编写一个自定义组件。为了协商Android中自定义视图的宽度和高度,我们重写了onMeasure()。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {}在HarmonyOS中,有什么可供选择的方法呢?
发布于 2021-07-26 05:51:10
在Android中,onMeasure()是一个在视图类中可用的受保护的方法,因此可以通过扩展视图类在定制组件中直接覆盖它。
作为HarmonyOS中的另一种选择,您必须在自定义组件中实现Component.EstimateSizeListener,然后用overriden函数onEstimateSize编写实现。
所以在Android中,当你有这样的代码时-
public class CustomView extends View {
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
}
}协调代码更改将是-
public class CustomComponent implements ComponentContainer.EstimateSizeListener
public CustomComponent(Context context, AttrSet attrSet, String styleName) {
super(context, attrSet, styleName);
setEstimateSizeListener(this); //set the listener here
}
@Override
public boolean onEstimateSize(int widthEstimatedConfig, int heightEstimatedConfig) {
}
}https://stackoverflow.com/questions/68524730
复制相似问题