【发布时间】:2013-11-25 10:46:12
【问题描述】:
当用户按下列表视图项目时,它会突出显示,并在松开时返回其原始颜色。 我想以编程方式查询这些是什么颜色。
在 14 之后的 Api 版本上,我似乎可以使用 (i) Theme.resolveAttribute(int resid, TypedValue outValue, boolean resolveRefs) 或 (ii) Theme.obtainStyledAttributes(int[] attrs)
//-- (i) ---------------------------------------------
int getAttrVal(int attr)
{
TypedValue Val = new TypedValue();
getContext().getTheme().resolveAttribute(attr, Val, true);
return Val.data;
}
int colBgd = getAttrVal(android.R.attr.colorBackground);
int colAh = getAttrVal(android.R.attr.colorActivatedHighlight);
int colPh = getAttrVal(android.R.attr.colorPressedHighlight); // not sure if this or prev is what im looking for!?
// (ii) ---------------------------------------------
TypedArray Arr = getContext().getTheme().obtainStyledAttributes(
new int[] {android.R.attr.colorPressedHighlight});
int colPh2 = arrTst.getColor(0, 0xFF00FF);
//--------------------------------------------------
在较旧的 Api 版本上,这将不起作用,因为 android.R.attr.colorActivatedHighlight/colorPressedHighlight 还没有。
(1) 是否可以在 api 8 上以编程方式获取这些颜色? (2) 我对 Theme 中的其他一些功能有点困惑
例如 (iii) Theme.obtainStyledAttributes(int resid, int[] attrs)
// ---------------------------------------------
TypedArray Arr2 = getContext().getTheme().obtainStyledAttributes(
android.R.attr.colorBackground, new int[] {android.R.attr.colorPressedHighlight});
int colTst2 = arrTst.getColor(0, 0xFF00FF);
Here it seems not to matter what value I use for resid. What's the point of this parameter?
(3) R.attr 和 R.styleable 有什么区别? R.attr 应该是 R.styleable 的替代品(并且基本上有相应的条目),还是它们的目的只是部分重叠。
【问题讨论】: