【发布时间】:2015-10-03 12:29:32
【问题描述】:
我有自定义视图,它将属性集(xml 值)作为构造函数值
public CustomView(Context context) // No Attributes in this one.
{
super(context);
this(context, null, 0);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
this(context, attrs, 0)
}
public CustomView(Context context, AttributeSet attrs, int default_style) {
super(context, attrs, default_style);
readAttrs(context, attrs, defStyle);
init();
}
在 Fragment 类中,我将视图设置为
CustomView customView = (CustomView) view.findViewById(R.id.customView);
自定义视图包含各种值,例如高度、宽度、填充等。
我想根据所需条件修改这些值并将其设置回自定义视图。 我在 onDraw 方法中放置了设置宽度高度代码并调用了无效视图。 但是如果我在 CustomView 类中调用 invalidate 方法,上述方法将设置每次。 如何克服这个问题,以便我只能在构造函数中传递修改后的属性集值。?
编辑:我需要修改在属性构造函数期间设置的视图值(使用新值初始化),以便获得具有新值的刷新视图。 覆盖 @OnDraw 或 'Invalidate' 对我来说不是一个好的功能,在 invalidate 内部我已经编写了将在每秒间隔执行的方法。
【问题讨论】:
-
尝试为您设置
LayoutParamsCustomView。这样您就可以根据需要的条件修改其值。 -
@astuter CustomView 的值不仅是宽度,高度等。在视图中定义的值,例如颜色,showText,ShowColor,ShowGraph。等等
标签: android android-custom-view android-custom-attributes