カスタム TextView があり、コンストラクターで、対応する xml レイアウトで特定の XML プロパティが宣言されているかどうかを確認したいと考えています。
<com.stuff.CustomTextView
android:id="@+id/tv01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="8sp" />
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
// find out if textSize was defined
if (!wasAttributeDefined(attrs,"textSize")) // EDIT
setTextSize(10); // EDIT
}
これが有効かどうかはわかりませんが、次のことを試しました(運が悪い):
attrs.getAttributeFloatValue("android","textSize",(float)-1.0);
ありがとう。
編集:
まあ、誰かが興味を持っているなら、これを解決する (おそらくあまり良くない) 方法があります。次のようなもの:
private boolean wasAttributeDefined(AttributeSet attrs, String name) {
for (int i=0; i<attrs.getAttributeCount(); i++)
if (attrs.getAttributeName(i).equals(name))
return true;
return false;
}