Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中更改属性集值_Android_Android Custom View_Android Custom Attributes - Fatal编程技术网

在android中更改属性集值

在android中更改属性集值,android,android-custom-view,android-custom-attributes,Android,Android Custom View,Android Custom Attributes,我有一个自定义视图,它将属性集(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,

我有一个自定义视图,它将属性集(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方法中,并调用invalidte视图。 但是,如果我在CustomView类中调用invalidate方法,上述方法将设置每次调用的时间。 如何克服这个问题,以便我只能在构造函数中传递修改后的属性集值

编辑:我需要修改在属性构造函数期间设置的视图值(使用新值初始化),以便获得具有新值的刷新视图。
Override@OnDraw或“Invalidate”对我来说不是一个好功能,因为在Invalidate中,我编写了将在每一秒间隔内执行的方法。

这可能不是您希望的解决方案,但是在xml中放置一个框架布局,而不是自定义视图,然后以编程方式创建您的自定义视图,将框架布局作为其父视图

,假设您为名为StarsView的视图声明了这样的自定义属性

<declare-styleable name="StarsView">
    <attr name="stars" format="integer" />
    <attr name="score" format="float" />
</declare-styleable>

我发现您的
CustomView
可以有多个属性,您希望根据某些条件修改其中一些属性,并将其传递给构造函数

设计自定义视图时很少有最佳做法:

  • 如果您有自定义属性,请确保通过setter和getter公开它们。在setter方法中,调用
    invalidate()
  • 不要尝试修改
    onDraw()
    onMeasure()方法中的任何属性
  • 尽量避免为自定义视图编写自定义构造函数

  • 因此,解决问题的理想方法是实例化CustomView,然后在外部(在活动或片段中)修改属性,或者在
    CustomView.java
    中有一个方法,然后在外部调用它。这样做仍然会得到与您所期望的相同的结果。

    而不是尝试为您设置
    LayoutParams
    CustomView
    。这样,您可以根据所需条件修改其值。@astuter自定义视图的值不仅是宽度、高度等,还包括在视图中定义的值,如颜色、showText、ShowColor、ShowGraph。等等,谢谢。我的问题是需要传递自定义参数,例如Customview.angle、Customview.Isentialized,就像以前在attributeset xml中设置的参数一样。在我的情况下,读取是可以的;问题是我需要根据条件更改值,并只刷新那些值。我有十个属性,我只需要修改两个属性,应该刷新视图。我想我一点也不明白,构建视图时收到的属性集只是Android允许您读取XML写入值的一种方式,当然,当我读取stars=5时,在textView 5中要写什么我会在创建set view时设置这个值,也会为视图中表示这个属性的变量添加set/get方法,这样您就可以修改它们了。这就是我要找的。我需要修改自定义属性值,以便使我的视图无效以反映在中。我建议您在CustomView中有一个方法,该方法执行您想要的条件检查(如果是泛型的),并在此基础上通过属性的setter方法修改您的属性。希望这有帮助。
    <my.package..StarsView
        app:stars="5"
        app:score="4.6"
    
    public StarsView(Context context, AttributeSet attrs) {
        super(context, attrs);
        if(attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.StarsView, defStyleAttr, 0);
            stars = Tools.MathEx.clamp(1, 10, a.getInt(R.styleable.StarsView_stars, 5));
            score =  (int)Math.floor(a.getFloat(R.styleable.StarsView_score, stars) * 2f);
            a.recycle(); // its important to call recycle after we are done
        }
    }