目次
リソースファイルを取得するための1つ
1.リソースファイルの名前でリソースIDを取得します
数日前にToastをカスタマイズしたとき、システムのネイティブToast UIを使用できるようにするために、Toastレイアウトファイルを参照する方法を考えていました。Toastのソースコードを確認すると、Toastのレイアウトファイルがtransient_notificationであることがわかります。
public static Toast makeText(@NonNull Context context, @Nullable Looper looper,
@NonNull CharSequence text, @Duration int duration) {
Toast result = new Toast(context, looper);
LayoutInflater inflate = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
tv.setText(text);
result.mNextView = v;
result.mDuration = duration;
return result;
}
次に、このレイアウトファイルをカスタムToastのUIに直接参照できる場合は、システムToastと整合性を保つことができます。次に、リソースに非常に便利な方法があることを発見しました。
public int getIdentifier(String name, String defType, String defPackage)
その中で
name:リソースファイルの名前。サフィックスを追加する必要はありません。
defType:値、ドローアブル、レイアウト、アニメーション、文字列など、リソースファイルが配置されているディレクトリ。
defPackgage:リソースファイルが配置されているパッケージの名前です。
この魔法の方法を使用すると、リソースファイルを簡単に追跡できます。
したがって、カスタムToastでは、transient_notificationレイアウトファイルがコードに適用され、システムToastと一貫性のあるスタイルが実現されます。
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@LayoutRes int layout = mContext.getResources().getIdentifier("transient_notification", "layout", "android");
View v = inflater.inflate(layout, null);
mMessageView = v.findViewById(android.R.id.message);
2.コードを簡素化します
実際、getResources()。getIdentifier()メソッドを使用すると、コードを簡略化することもできます。たとえば、フレームアニメーションAnimationDrawableを使用しています(フレームアニメーションを使用する過程でOOMの問題が発生しますが、getResources( ).getIdentifier()別の使用法)、2つの通常の方法があります:
- 方法1
(1)ドローアブルの下にframe.xmlファイルを定義し、ドローアブルファイルをImageViewに直接割り当ててから、AnimationDrawable anim =(AnimationDrawable)ivFrame.getDrawable();を使用してフレームアニメーションを取得し、再生します。この記事で紹介した内容ではないので、詳しくは説明しません。
- 方法2:
(1)レイアウトファイルでImageViewコントロールを定義します
<ImageView
android:id="@+id/iv_frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/frame" />
(2)コードを使用してJavaコードでアニメーションを再生する
public void animationDrawable(View view) {
AnimationDrawable drawable = new AnimationDrawable();
drawable.addFrame(getDrawable(R.drawable.frame00), 200);
drawable.addFrame(getDrawable(R.drawable.frame01), 200);
drawable.addFrame(getDrawable(R.drawable.frame02), 200);
drawable.addFrame(getDrawable(R.drawable.frame03), 200);
drawable.addFrame(getDrawable(R.drawable.frame04), 200);
drawable.addFrame(getDrawable(R.drawable.frame05), 200);
drawable.addFrame(getDrawable(R.drawable.frame06), 200);
drawable.addFrame(getDrawable(R.drawable.frame07), 200);
drawable.addFrame(getDrawable(R.drawable.frame08), 200);
drawable.addFrame(getDrawable(R.drawable.frame09), 200);
drawable.addFrame(getDrawable(R.drawable.frame10), 200);
drawable.addFrame(getDrawable(R.drawable.frame11), 200);
drawable.start();
}
このメソッドのコードには、実際にはいくつかの通常のリソースファイルframe00〜frame11を追加するための冗長なコードがあります。実際、このgetResources()。getIdentifier()を使用してコードを簡略化し、コードをより簡潔にすることができます。変換後は次のようになります:
public void animationDrawable(View view) {
AnimationDrawable drawable = new AnimationDrawable();
for (int i = 0; i < 12; i++) {
String index = i < 10 ? ("0" + i) : String.valueOf(i);
int drawableId = getResources().getIdentifier(String.format("frame%s", index), "drawable", getPackageName());
Drawable db = getDrawable(drawableId);
drawable.addFrame(db, 200);
}
drawable.start();
}
瞬時に、10行のコードが5行のコードに削減され、コードがはるかに簡潔になりました。
3.まとめ
getResources()。getIdentifier()メソッドは、リソース名がわかっている場合に対応するリソースIDを取得できます。リソースIDを使用すると、対応するgetメソッドに従って対応するコンテンツを取得できます。さらに、コードを単純化し、コードを簡潔に保つために、いくつかの通常のリソースファイルをロードできます。
次に、システムプロパティを取得します
Android View属性設定の優先度の概要では、getStyledAttributesを使用してカスタム属性を取得できることも記載されています。したがって、レイアウトファイルでいくつかのシステム属性値を取得したい場合、それをどのように処理する必要がありますか?もちろん、呼び出すシステムメソッドがいくつか用意されています。たとえば、カスタムビューでは、layout_widthの値を取得する必要があり、getMeasuredWidth()を使用して対応する値を取得できますが、取得する必要がある場合は、コンストラクター値はどうですか?コンストラクターが呼び出されたときにカスタムビューがonMeasure()メソッドに到達しておらず、getMeasuredWidth()値が0であるため、getStyledAttributesを使用してlayout_width値を取得できます。
public final TypedArray obtainStyledAttributes(
@Nullable AttributeSet set, @NonNull @StyleableRes int[] attrs)
このメソッドでは、最初のパラメーターセットはレイアウトファイルでビューを定義するときに設定されたレイアウトパラメーターであり、2番目のパラメーターattrsは取得したい属性値の配列です。layout_widthの値を取得したい場合、次にlayout_widthを配列に入れて、2番目のパラメーターを渡す必要があります。
private void initAttribute(AttributeSet attrs, int defStyleAttr, int defStyleRes) {
if (attrs == null) {
return;
}
int[] attrsArray = new int[]{android.R.attr.layout_width, android.R.attr.background};
TypedArray array = getContext().obtainStyledAttributes(attrs, attrsArray);
if (array == null) {
return;
}
//第一个参数与传入的attrsArray的索引值对应
int layout_width = array.getLayoutDimension(0, -1);
Drawable background = array.getDrawable(1);
}
3つの要約
現在、Androidの一部のコンテンツは本当に楽しいことがますますわかっており、ソースコードをさらに調査する必要があります。いい加減にして