2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > andriod 获取attrs_Android:具有attrs.xml中对象引用的Custom View ...

andriod 获取attrs_Android:具有attrs.xml中对象引用的Custom View ...

时间:2022-07-09 01:51:39

相关推荐

andriod 获取attrs_Android:具有attrs.xml中对象引用的Custom View ...

我试图建立对象之间的关系层次结构.每个对象都具有与其自身相同类型的父对象,或者为null.

我有一个main.xml,其中包含其中一些:

android:id="@+id/bi"

android:layout_weight="1"

custom:code=".."

custom:parentKey="@id/be"

android:text="@string/i" />

包含以下之一的res / values / attrs.xml:

和一个包含以下内容的类(不是我的活动):

public class MorseKey extends Button {

public MorseKey(Context context, AttributeSet attrs) {

super(context, attrs);

initMorseKey(attrs);

}

private void initMorseKey(AttributeSet attrs) {

TypedArray a = getContext().obtainStyledAttributes(attrs,

R.styleable.MorseKey);

final int N = a.getIndexCount();

for (int i = 0; i < N; i++) {

int attr = a.getIndex(i);

switch (attr)

{

case R.styleable.MorseKey_code:

code = a.getString(attr);

break;

case R.styleable.MorseKey_parentKey:

parent = (MorseKey)findViewById(a.getResourceId(attr, -1));

//parent = (MorseKey)findViewById(R.id.be);

Log.d("parent, N:", ""+parent+","+N);

break;

}

}

a.recycle();

}

private MorseKey parent;

private String code;

}

这不起作用.每个MorseKey实例报告N == 2(好),而父== null(坏).更多,parent == null,即使我明确尝试将其设置为任意值(请参见注释).我也尝试过custom:parentKey =“ @ id / be”(带有加号),但也没有用.我究竟做错了什么?

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。