---
title: "在代码中获取Android theme中的attr属性值"
author: "OliveExcel"
date: "2015-07-16T20:31:24Z"
tags: []
categories: ["移动开发"]
slug: "android-theme-attr"
draft: true
lastmod: "2015-07-16T20:31:24Z"
id: 2227992
url: "https://oliveexcel.iteye.com/blog/2227992"
---

Android的Theme是由各种attr组合而成, 每个attr对应了这个属性的一个引用, 这个引用又可以是各种东西.

 

在某些情况下, 我们需要获取非自定义的主题下某个属性的内容 (比如拿到系统默认的配色colorAccent), 操作方式举例一则:

```
int defaultColor = 0xFF000000;
int[] attrsArray = { andorid.r.attr.colorAccent };
TypedArray typedArray = context.obtainStyledAttributes(attrsArray);
int accentColor = typedArray.getColor(0, defaultColor);

// don't forget the resource recycling
typedArray.recycle();
```