2017-09-12 154 views
1

我有一个View,此视图包含一个Point。我想将数据绑定到这一点,但据我所知,我无法在attrs.xml中创建一个自定义属性,其格式为Point将自定义视图绑定到点

这是视图:

public class MyView extends android.support.v7.widget.AppCompatImageView { 

    private Point point; 

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

    public Point getPoint() { 
     return point; 
    } 

    public void setPoint(Point point) { 
     this.point = point; 
    } 
} 

这是为MyObject:

public class MyObject extends BaseObservable { 
    private Point point; 

    @Bindable 
    public Point getPoint() { 
     return point; 
    } 

    public void setPoint(Point point) { 
     this.point = point; 
     notifyPropertyChanged(BR.point); 
    } 
} 

现在我要绑定MyView的是这样的:

<MyView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:point="@{myObject.point}"/> 

attrs.xml文件像这样:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="MyView"> 
     <attr name="point" format="Point" /> 
    </declare-styleable> 
</resources> 

但这似乎不可能。有人知道解决方案吗?

回答

0

我找到了解决此问题的方法。相反,结合一个Point,我创建

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="MyView"> 
     <attr name="pointX" format="integer" /> 
     <attr name="pointY" format="integer" /> 
    </declare-styleable> 
</resources> 

然后,我必然的myObject.getPoint().x值来pointXmyObject.getPoint().ypointY。这不是最美丽的解决方案,但它的工作原理。

相关问题