在为Android开发时,如何正确定义自定义XML属性并调用它们?

问题描述

| 在我的attrs.xml中,我有以下内容
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<resources>
    <declare-styleable name=\"GameButtons\">         
        <attr name=\"row\" format=\"integer\" />
        <attr name=\"col\" format=\"integer\" />
    </declare-styleable>
</resources>
在我的main.xml文件中,我具有以下内容(com.example.helloandroid是清单中定义的包):
<ImageButton
    xmlns:whatever=\"http://schemas.android.com/apk/res/com.example.helloandroid\"
    android:id=\"@+id/sp11\"
    android:layout_width=\"wrap_content\"
    android:layout_height=\"wrap_content\"
    android:layout_centerHorizontal=\"true\"
    android:src=\"@drawable/button_imgs\"
    android:background=\"#ffffffff\"
    android:onClick=\"Selected\"
    whatever:row=\"1\"
    whatever:col=\"1\"
/>
在GameButtons.java中,我有:
public class GameButtons extends ImageButton {

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

public GameButtons(Context context,AttributeSet attrs) {
    super(context,attrs);

    TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.GameButtons);

    int selected_row = a.getInteger(R.styleable.GameButtons_row,0);
    int selected_col = a.getInteger(R.styleable.GameButtons_col,0);

    check_move (selected_row,selected_col);

    a.recycle();
}
我的问题是...什么时候调用SugarPackets构造函数?或如何获取check_move函数? 我不确定是否为ImageButton正确定义了自定义属性。基本上,我想添加两个自定义属性:row和col。我还有一个onClick事件处理程序,其中:
GameButtons test = (GameButtons) button;
但它不会调用构造函数。最终,我需要在单击ImageButton的行和列的情况下调用check_move。救命?!谢谢!     

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)