在我的活动中,我正在维护一个
SuperActivity
,并在其中设置主题。public class SuperActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.MyTheme);
}
}
themes.xml
<!-- ImageBackround -->
<style name="Theme.MyTheme" parent="ThemeLight">
<item name="myBgColor">@color/translucent_black</item>
</style>
现在我想在我的子活动中获取这种颜色。
正如在这个可能的答案中提到的那样,我写了以下内容:
int[] attrs = new int[] { R.attr.myBgColor /* index 0 */};
TypedArray ta = ChildActivity.this.obtainStyledAttributes(attrs);
int color = ta.getColor(0, android.R.color.background_light);
String c = getString(color);
ta.recycle();
但我每次都获取到android.R.color.background_light
的默认值而非R.attr.myBgColor
的值。
我做错了什么?是我传递的ChildActivity.this
上下文有误吗?