所以我环顾四周,发现android.R.styleable
不再是SDK的一部分,尽管它仍然是文档中的here。
如果清楚地记录了替代方案是什么,那就不是问题了。例如,AOSP Calendar App仍在使用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();
那么,如果不从android.R.styleable.Theme
获取int[]
,如何获得backgroundDimAmount
呢?
我需要在obtainStyledAttributes(int [])
中做些什么才能让它与SDK一起工作?
发布于 2010-01-25 14:11:49
CustomView应用编程接口演示展示了如何检索样式化属性。视图的代码如下:
用于检索文本、颜色和大小的可设置样式的数组在下面的<declare-styleable>
部分中定义:
您可以使用<declare-styleable>
定义要作为一个组检索的任何属性列表,其中既包含您自己的属性列表,也包含平台定义的属性列表。
就文档中的这些内容而言,围绕可样式化数组有大量的java文档,这使得它们在文档中很有用,所以它们被留在了文档中。但是,随着数组的变化,例如添加新的属性,常量的值可能会改变,因此平台常量不能在SDK中(请不要使用任何技巧来访问它们)。无论如何,应该不需要使用平台,因为它们每个都只是为了实现框架的一部分,并且像这里所示的那样创建自己的平台是很容易的。
发布于 2010-09-22 04:49:11
在这个例子中,他们省略了对上下文‘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;
}
将obtainStyledAttributes更改为c.obtainStyledAttributes应该可以
发布于 2014-08-17 09:57:19
在自定义视图中提取标准属性(背景)的示例,该视图具有自己的默认样式。在此示例中,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>
https://stackoverflow.com/questions/2127177
复制相似问题