كيف يمكنني استخدام الحصول على styledattributes (int []) مع الموضوعات الداخلية لنظام Android
-
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>
لتحديد أي قائمة من السمات التي تريد استردادها كمجموعة ، تحتوي على كل من خاصتك والسلالات المحددة بواسطة النظام الأساسي.
بقدر ما تكون هذه الأشياء في الوثائق ، هناك الكثير من Doc 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;
}
يجب تغيير الحصول على stystyledattributes إلى c.oBtainStyledattributes
مثال على سحب السمة القياسية (الخلفية) في طريقة عرض مخصصة لها أسلوبها الافتراضي الخاص. في هذا المثال ، العرض المخصص PasswordGrid يمتد Gridlayout. لقد حددت نمطًا لـ PasswordGrid الذي يعين صورة خلفية باستخدام سمة Android القياسية Android: الخلفية.
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. لقد قدمت مشكلة على ذلك ، والتي قد ترغب في نجمة لتلقي التحديثات عليها.
كقائد عمل ، يمكنك استخدام الانعكاس للوصول إلى الحقل:
Class clazz=Class.forName("android.R$styleable");
int i=clazz.getField("Theme_backgroundDimAmount").getInt(clazz);