2017-08-28 110 views
1

如何获取参考颜色的实际值?在布局我可以使用下面的...如何以编程方式获取colorAccent等内置资源的价值?

android:textColor="?android:attr/colorAccent" 

..和这部作品中设置一个TextView的主题定义强调色的文本颜色。如何在运行时获得使用colorAccent代码的价值?

此外,你如何发现所有可用值的列表,必须有一长串可用的颜色列表,我可以找到,但列表定义在哪里?

回答

1

如果资源是一个Android定义之一:

var id = Android.Resource.Attribute.ColorAccent; 

如果资源是一个对话框,窗口小部件等内..这不是一个Android系统资源(即,得到DatePickerDialog资源)

var id = SomeDatePickerDialog.Resources.GetIdentifier("date_picker_header_date", "id", "android"); 

使用得到的ID:

var typedArray = Theme.ObtainStyledAttributes(new int[] { id }); 
    var color = typedArray.GetColor(0, int.MaxValue); 
    if (color != int.MaxValue) 
    { 
     Log.Debug("COLOR", color.ToString()); 
    } 

与API /主题的R名单的变化,可用的基础值:

但你必须使用Android源的API的完整参考你正在看:

在奥利奥测试中定义的颜色一般:

然后查看特定颜色xml文件中的定义方式,并使用该定义查找其实际值(在valueXXX文件中的一个中)....

2

对于例如,你有你可以得到价值的东西是这样的:

//default color instead the attribute is not set. 
var color = Color.Blue; 

var attributes = new int[] { Android.Resource.Attribute.ColorAccent }; 
var typeArray = ObtainStyledAttributes(attributes); 

//get the fist item (we are sending only one) and passing 
//the default value we want, just in case. 
var colorAccent = typeArray.GetColor(0, color); 

colorAccent将有Color集的主题为ColorAccent属性,如果任何或默认值。

重要的问题是该方法ObtainStyledAttributesContext的一部分,所以如果你已经在一个活动中,你会发现它作为它的一部分,但如果你在其他任何类,你将需要在情况背景下通过它不可用。

有关可用值的完整列表,您可以从Android.Resource.Attribute类中获得。在VS做一个检查,看看这个类有不同的属性。也许Android文档有更好的方法。

希望这helps.-

相关问题