Android の内部テーマで getStyledAttributes(int []) を使用するにはどうすればよいですか
-
22-09-2019 - |
質問
それで周りを見回してわかったことは、 android.R.styleable
まだドキュメント化されていますが、SDK の一部ではなくなりました。 ここ.
代替手段が何であるかが明確に文書化されていれば、それほど問題にはなりません。たとえば、AOSP カレンダー アプリはまだ android.R.styleable
// Get the dim amount from the theme
TypedArray a = obtainStyledAttributes(com.android.internal.R.styleable.Theme);
lp.dimAmount = a.getFloat(android.R.styleable.Theme_backgroundDimAmount, 0.5f);
a.recycle();
では、どうやってそれを手に入れるのでしょうか? backgroundDimAmount
を取得せずに int[]
から android.R.styleable.Theme
?
何を突っ込まなければならないのか obtainStyledAttributes(int [])
SDKで動作させるには?
解決
CustomView API デモでは、スタイル付き属性を取得する方法を示します。ビューのコードは次のとおりです。
テキスト、色、サイズの取得に使用されるスタイル可能な配列は、 <declare-styleable>
ここのセクション:
使用できます <declare-styleable>
独自の属性とプラットフォームによって定義された属性の両方を含む、グループとして取得する属性のリストを定義します。
これらのことがドキュメントに記載されている限り、スタイル可能な配列に関する多くの Java ドキュメントがあり、それらをドキュメントに含めておくと便利なので、それらはそのまま残されています。ただし、新しい属性が追加されるなど、配列が変更されると、定数の値が変わる可能性があるため、プラットフォームの定数を SDK に含めることはできません (また、それらにアクセスするためにトリックを使用しないでください)。いずれにせよ、プラットフォームのものを使用する必要はありません。なぜなら、それらはそれぞれフレームワークの一部を実装するためだけに存在しており、ここで示すように独自のものを作成するのは簡単だからです。
他のヒント
は、例えば、それらはコンテキスト「C」への参照を残し
public ImageAdapter(Context c) {
TypedArray a = c.obtainStyledAttributes(R.styleable.GalleryPrototype);
mGalleryItemBackground = a.getResourceId(
R.styleable.GalleryPrototype_android_galleryItemBackground, 0);
a.recycle();
return mGalleryItemBackground;
}
c.obtainStyledAttributesにobtainStyledAttributesを変更すると、動作するはずです。
独自のデフォルト スタイルを持つカスタム ビューで標準属性 (背景) を抽出する例。この例では、カスタム ビュー パスワードグリッド 伸びる グリッドレイアウト. 。標準の Android 属性を使用して背景画像を設定する PasswordGrid のスタイルを指定しました アンドロイド:背景.
public class PasswordGrid extends GridLayout {
public PasswordGrid(Context context) {
super(context);
init(context, null, 0);
}
public PasswordGrid(Context context, AttributeSet attrs) {
super(context, attrs, R.attr.passwordGridStyle);
init(context, attrs, 0);
}
public PasswordGrid(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs, defStyle);
}
private void init(Context context, AttributeSet attrs, int defStyle) {
if (!isInEditMode()) {
TypedArray stdAttrs = context.obtainStyledAttributes(attrs,
new int[] { android.R.attr.background }, // attribute[s] to access
defStyle,
R.style.PasswordGridStyle); // Style to access
// or use any style available in the android.R.style file, such as
// android.R.style.Theme_Holo_Light
if (stdAttrs != null) {
Drawable bgDrawable = stdAttrs.getDrawable(0);
if (bgDrawable != null)
this.setBackground(bgDrawable);
stdAttrs.recycle();
}
}
}
以下は、styles.xml ファイルの一部です。
<declare-styleable name="passwordGrid">
<attr name="drawOn" format="color|reference" />
<attr name="drawOff" format="color|reference" />
<attr name="pathWidth" format="integer" />
<attr name="pathAlpha" format="integer" />
<attr name="pathColor" format="color" />
</declare-styleable>
<style name="PasswordGridStyle" parent="@android:style/Widget.GridView" >
<!-- Style custom attributes. -->
<item name="drawOff">@drawable/ic_more</item>
<item name="drawOn">@drawable/ic_menu_cut</item>
<item name="pathWidth">31</item>
<item name="pathAlpha">129</item>
<item name="pathColor">@color/green</item>
<!-- Style standard attributes -->
<item name="android:background">@drawable/pattern_bg</item>
</style>
これは、SDKのバグのように見えます。私はあなたが望むことができる、その上に問題を、提出しましたその上で更新を受信するように主演する。
worksaroundとして、あなたはフィールドにアクセスするためにリフレクションを使用することができます:
Class clazz=Class.forName("android.R$styleable");
int i=clazz.getField("Theme_backgroundDimAmount").getInt(clazz);