2017-02-23 35 views
2

TextViewとButtonにVectorDrawableをDrawableLeft、DrawableRightなどに設定するメソッドがありますが、通常のDrawableでも同じようにしたいと思います。ですから、DrawableResの型をチェックする方法はありますか?またはtry/catchを使うべきですか? この方法は次のようになります。DrawableResがVectorDrawableかどうか確認してください

public static void setViewDrawables(@NonNull View view, @DrawableRes int left, @DrawableRes int top, @DrawableRes int right, @DrawableRes int bottom) { 
    final Resources resources = view.getResources(); 
    Drawable vectorDrawableLeft = left != 0 ? VectorDrawableCompat.create(resources, left, view.getContext().getTheme()) : null; 
    Drawable vectorDrawableTop = top != 0 ? VectorDrawableCompat.create(resources, top, view.getContext().getTheme()) : null; 
    Drawable vectorDrawableRight = right != 0 ? VectorDrawableCompat.create(resources, right, view.getContext().getTheme()) : null; 
    Drawable vectorDrawableBottom = bottom != 0 ? VectorDrawableCompat.create(resources, bottom, view.getContext().getTheme()) : null; 

    if (view instanceof Button) { 
     ((Button) view).setCompoundDrawablesWithIntrinsicBounds(vectorDrawableLeft, vectorDrawableTop, vectorDrawableRight, vectorDrawableBottom); 
    } 
    else if (view instanceof TextView) { 
     ((TextView) view).setCompoundDrawablesWithIntrinsicBounds(vectorDrawableLeft, vectorDrawableTop, vectorDrawableRight, vectorDrawableBottom); 
    } else { 
     Log.e("ERROR: ViewUtils", "Can't do setCompoundDrawablesWithIntrinsicBounds on " + view.getClass().getName()); 
    } 
} 
+2

ので、あなたは 'android.support.v7.content.res.AppCompatResources 'たい:それはこのようになりますか? – pskink

+0

それは働いています。ありがとう!私はちょうど 'AppCompatResources.getDrawable(view.getContext()、left)'で 'VectorDrawableCompat.create(resources、left、view.getContext()。getTheme())'を置き換えました。 –

+0

また、 'android.support.v4.content.res.ResourcesCompat 'もありますが、それがうまくいくかどうかは分かりませんが、あなたはそれをチェックすることができます... – pskink

答えて

1

コメントを回答に要約します。私が知る限り、DrawableResがVectorDrawableを指しているかどうかを簡単に知る方法はありません。誰かがどのように知っているか書き留めてください。しかし、私の問題を解決するには、@ pskinkのようにandroid.support.v7.content.res.AppCompatResourcesを使うだけで十分でした。だから今の方法は、次のようになります。

public static void setViewDrawables(@NonNull View view, @DrawableRes int left, @DrawableRes int top, @DrawableRes int right, @DrawableRes int bottom) { 
    Drawable vectorDrawableLeft = left != 0 ? AppCompatResources.getDrawable(view.getContext(), left) : null; 
    Drawable vectorDrawableTop = top != 0 ? AppCompatResources.getDrawable(view.getContext(), top) : null; 
    Drawable vectorDrawableRight = right != 0 ? AppCompatResources.getDrawable(view.getContext(), right) : null; 
    Drawable vectorDrawableBottom = bottom != 0 ? AppCompatResources.getDrawable(view.getContext(), bottom) : null; 

    if (view instanceof Button) { 
     ((Button) view).setCompoundDrawablesWithIntrinsicBounds(vectorDrawableLeft, vectorDrawableTop, vectorDrawableRight, vectorDrawableBottom); 
    } else if (view instanceof TextView) { 
     ((TextView) view).setCompoundDrawablesWithIntrinsicBounds(vectorDrawableLeft, vectorDrawableTop, vectorDrawableRight, vectorDrawableBottom); 
    } else { 
     Log.e("ERROR: ViewUtils", "Can't do setCompoundDrawablesWithIntrinsicBounds on " + view.getClass().getName()); 
    } 
} 

更新:android.support.v7.widget.AppCompatDrawableManager

がDrawableのがVectorDrawableであるかどうかをチェックされているメソッドboolean isVectorDrawable(@NonNull Drawable d)ですが、それはDrawableResを引数としてDrawableのを取って、そしていません。

private static boolean isVectorDrawable(@NonNull Drawable d) { 
    return d instanceof VectorDrawableCompat 
      || PLATFORM_VD_CLAZZ.equals(d.getClass().getName()); 
} 
+1

彼らがどのようにそれをしたのか知りたいのであれば、デバッガで 'AppCompatResources.getDrawable'に挿入し、' VectorDrawableCompat'を作成しなければならないときに 'VectorDrawableCompat.create'を呼び出すタイミングを確認してください。 – pskink

+0

' isVectorDrawable'メソッドの直後の行の数だけ 'private static class VdcInflateDelegate implements InflateDelegate'これは 'VectorDrawableCompat'オブジェクトを膨らませます。 – pskink

+0

@pskinkはい、それはtry/catchのアプローチです。私は疑問を尋ねました。その方法では、VectorDrawableを作成しようとしていますが、それが動作していない場合はVectorDrawableではありません。 –

関連する問題