2012-04-05 24 views
0

私はカスタムTextViewを持っていて、コンストラクタ内で、特定のXMLプロパティが宣言されているかどうかを、対応する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); 

感謝。

EDIT:誰もが興味を持っている場合

も、これを解決するための(おそらくあまりよくない)方法があります。

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; 
} 
+0

は、あなたのログを置いていましたか?エラーが発生しましたか? –

+0

エラーはありません。 XMLにアンドロイド:textSizeを定義したにもかかわらず、私が提供したデフォルト値(-1.0)を得ているだけです。 – takecare

答えて

0

あなたが何か使用して属性をテストする必要があります:のようないろいろ書い

R.id.tv01.attr.textSize

+0

これは何ですか? – waqaslam

+0

これはID tv01のウィジェットの属性android:textSizeに設定された値を与えます – Manitoba

+0

これは、プロパティがXMLで宣言されているかどうかを知りたいときに、「新しい」CustomTextViewが出現するたびに、コンストラクタ内では、単一の/特定のものではなく – takecare

関連する問題