21

それで、私は周りを見回して、それがまだここ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();

では、 fromを取得backgroundDimAmountせずにどのように取得するのでしょうか。int[]android.R.styleable.Theme

obtainStyledAttributes(int [])SDKで動作させるには、何に固執する必要がありますか?

4

4 に答える 4

16

CustomView APIデモは、スタイル付き属性を取得する方法を示しています。ビューのコードは次のとおりです。

https://github.com/android/platform_development/blob/master/samples/ApiDemos/src/com/example/android/apis/view/LabelView.java

テキスト、色、サイズを取得するために使用されるスタイル可能な配列は、次の<declare-styleable>セクションで定義されています。

https://github.com/android/platform_development/blob/master/samples/ApiDemos/res/values/attrs.xml#L24

を使用<declare-styleable>して、独自の属性とプラットフォームで定義された属性の両方を含む、グループとして取得する属性のリストを定義できます。

これらがドキュメントに含まれている限り、スタイリング可能な配列の周りには多くのJavaドキュメントがあり、ドキュメントに含めると便利なので、そのまま残されています。ただし、新しい属性が追加されるなど、配列が変更されると、定数の値が変更される可能性があるため、プラットフォームの値をSDKに含めることはできません(トリックを使用して配列にアクセスしないでください)。プラットフォームのものは、フレームワークの一部を実装するためだけに存在するため、とにかく使用する必要はありません。ここに示すように、独自のプラットフォームを作成するのは簡単です。

于 2010-01-25T06:11:49.373 に答える
15

この例では、コンテキスト'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;
}

getStyledAttributesをc.obtainStyledAttributesに変更すると機能するはずです

于 2010-09-21T20:49:11.440 に答える
7

独自のデフォルトスタイルを持つカスタムビューで標準属性(背景)を引き出す例。この例では、カスタムビューPasswordGridがGridLayoutを 拡張します。標準のAndroid属性android:backgroundを使用して背景画像を設定する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>
于 2014-08-17T01:57:18.733 に答える
5

これはSDKのバグのようです。私はそれに関する問題を提出しました、あなたはそれに関する最新情報を受け取るためにあなたが主演したいと思うかもしれません。

回避策として、リフレクションを使用してフィールドにアクセスできます。

Class clazz=Class.forName("android.R$styleable");
int i=clazz.getField("Theme_backgroundDimAmount").getInt(clazz);
于 2010-01-24T14:37:06.433 に答える