【问题标题】:xamarin for android :How to setTextColor Set Xml Selectorxamarin for android:如何设置文本颜色设置 Xml 选择器
【发布时间】:2020-06-19 00:10:50
【问题描述】:

我希望 XML 颜色选择器在 Java 代码中设置 TextView

mText.setTextColor(getResources().getColorStateList(R.color.xml_color_selector))

此代码在 Xamarin 中如何工作?


我从here 1here 2 找到了API。我都试过了,但是:

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"

谢谢。

附:我想将 Java 代码转换为 C# 代码 并在代码中将 XML 选择器设置为 Textcolor

这是 Resources\Drawable\xml_color_selector.xml
我希望在Activity中设置这个TextColordrawable

<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>


此操作SetBackground 工作正常。

ListItemView.SetBackgroundResource(Resource.Drawable.menu_item_background_color_pressed);

【问题讨论】:

  • 找到了。 ListItemText.SetTextColor(Context.Resources.GetColorStateList(Resource.Color.menu_item_title_color_selector_activated));

标签: android xamarin textview xamarin.android


【解决方案1】:

这也让我很头疼。由于更直观的 GetColor(int) 已被 Google 弃用,您不能再这样做了。

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));

希望解释有所帮助。

【讨论】:

    【解决方案2】:

    你可以在xml中设置它 如果你想选择它作为背景颜色然后写

      android:background="@color/xml_color_selector"
    

    并且您的xml_color_selector.xml 文件应该在 res 的 color 文件夹中。

    如果您只想添加边框,则应将 xml_color_selector.xml 保留在可绘制文件夹中,然后写入

    android:background="@drawable/xml_color_selector"
    

    希望对你有所帮助。

    【讨论】:

    • 我希望在我的代码中更改文本颜色资源,而不是 axml 设置
    【解决方案3】:
    mText.SetBackgroundColor (Color.Transparent);
    

    确保 Color 是资源目录中的一个 xml 文件(资源 -> 值 -> 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>
    

    【讨论】:

    • 那是 xamarin 代码。来自我使用 Xamarin 开发的 Android 应用程序,并且在应用程序中。 Xamarin 使用资源文件。
    猜你喜欢
    • 2017-05-13
    • 1970-01-01
    • 2023-03-11
    • 2011-10-04
    • 2018-12-07
    • 1970-01-01
    • 1970-01-01
    • 2012-09-13
    • 1970-01-01
    相关资源
    最近更新 更多