I want XML color selector to set a TextView
in Java code.
mText.setTextColor(getResources().getColorStateList(R.color.xml_color_selector))
How does this code work in Xamarin?
I found the API from here 1 and here 2. I tried both of them, but:
mText.SetTextColor(Android.Content.Res.Resources. "not found GetColorStateList"<br>
mText.SetTextColor(Resources. "not found GetColorStateList"
mText.SetTextColor(Java.Lang.ClassLoader. "Not Found GetResource"
mText.SetTextColor(Java.Lang.Class. "Not Found GetResource"
Thanks.
P.S. I want to convert Java code to C# code
and set the XML selector to Textcolor
in code.
This is Resources\Drawable\xml_color_selector.xml
I hope Set this TextColor
drawable in Activity
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/menu_item_title_color_pressed" android:state_pressed="true" />
<item android:color="@color/menu_item_title_color" android:state_pressed="false" />
</selector>
This action SetBackground
works fine.
ListItemView.SetBackgroundResource(Resource.Drawable.menu_item_background_color_pressed);
答案 0 :(得分:0)
您可以在xml中设置它 如果你想选择它作为背景颜色,那么写
android:background="@color/xml_color_selector"
并且您的xml_color_selector.xml
文件应位于res。
如果你只想添加边框,那么你应该保留xml_color_selector.xmlin drawable文件夹,然后写
android:background="@drawable/xml_color_selector"
希望它会对你有所帮助。
答案 1 :(得分:0)
mText.SetBackgroundColor (Color.Transparent);
确保Color是资源目录中的xml文件(参数 - &gt; Values - &gt; Color.xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="solid_red">#fff000</color>
<color name="transparent">#00000000</color>
<color name="black">#000000</color>
<color name="lightgrey">#bbbbbb</color>
<color name="grey">#333333</color>
<color name="white">#ffffff</color>
<color name="listseparator">#2A3748</color>
<color name="yellow">#FECF35</color>
<color name="blue">#00f</color>
</resources>
答案 2 :(得分:0)
这也让我头疼。由于Google不推荐使用更直观的GetColor(int)
,因此您不能再这样做。
view.SetTextColor(context.Resources.GetColor(Resource.Color.color_name));
相反,您应该以以下任何一种方式使用GetColor(int, Theme)
view.SetTextColor(context.Resources.GetColor(Resource.Color.color_name, null));
view.SetTextColor(context.Resources.GetColor(Resource.Color.color_name, theme));
但是,此功能仅在以后的API 23中可用,因此,如果您想支持较旧的设备,许多Java开发人员都会建议使用
view.SetTextColor(ContextCompat.GetColor(context, Resource.Color.color_name));
现在,对于我们Xamarin开发人员来说,此将不会起作用,因为它将显示一条错误消息,指出无法将int
转换为ColorStateList
。因此,您应该使用
解决方案:
view.SetTextColor(ContextCompat.GetColorStateList(context, Resource.Color.color_name));
希望说明会有所帮助。