2017-02-09 2 views
0

related SO question複数のフォーマットのカスタム属性の値を読み取る方法は?

複数の種類のカスタム属性を定義できます。

<declare-styleable name="RoundedImageView"> 
    <attr name="cornerRadius" format="dimension|fraction"/> 
</declare-styleable> 

そして私はそれの外の値を読むために以下の方法

<RoundedImageView app:cornerRadius="30dp"/> 
<RoundedImageView app:cornerRadius="20%"/> 

どのようにそれを使用したいですか?


APIレベル21は、私は、これは推奨される解決策だと思いAPI TypedArray.getType(index)

int type = a.getType(R.styleable.RoundedImageView_cornerRadius); 
if (type == TYPE_DIMENSION) { 
    mCornerRadius = a.getDimension(R.styleable.RoundedImageView_cornerRadius, 0); 
} else if (type == TYPE_FRACTION) { 
    mCornerRadius = a.getFraction(R.styleable.RoundedImageView_cornerRadius, 1, 1, 0); 
} 

を提供します。

しかし、APIレベルを低くする方法はありますか? try catchを使用する必要がありますか?

また、2つの属性を定義することもできます。cornerRadiuscornerRadiusPercentage ... CSSでborder-radiusが恋しいです。

+0

'TypedArray.getValue(...)' + 'TypedValue.type'? – Selvin

+0

@Selvin私はあなたの解決策を試しています。私はそれがうまくいくと思います。ありがとう。 – Moon

+0

@Selvin回答を投稿することができます。私はそれを受け入れるでしょう。 – Moon

答えて

0

@ Selvinさんのコメントありがとうございます。 TypedArray.getValue(...)​​を使用できます。あなたは型定数を見つけることができますhere

+0

なぜ 'mCornerRadius = tv.getFraction(1,1);および' mCornerRadius = tv.getDimension(getContext()。getResources()。getDisplayMetrics()); '? – Selvin

+0

@Selvinありがとう!私は仕事を急ぐ...更新されました。 – Moon

関連する問題