【问题标题】:xamarin set view background color from a hex valuexamarin 从十六进制值设置视图背景颜色
【发布时间】:2015-06-23 10:15:11
【问题描述】:

我如何使用十六进制值中的 xamarin 以编程方式设置视图的背景颜色

例如

 view.BackgroundColor = Color.FromHex("#00162E");

【问题讨论】:

标签: xamarin xamarin.android


【解决方案1】:

从他们的例子..我认为你只需要删除#。

view.BackgroundColor = Color.FromHex("FF6A00")

http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/colors/

根据您的目标,您可能想尝试在颜色类上使用 parseColor 方法。

How to get a Color from hexadecimal Color String

public static int parseColor (String colorString)

它声称采用十六进制值:

... formats are: #RRGGBB #AARRGGBB 'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow', 'lightgray', 'darkgray'

【讨论】:

  • 我收到一条错误消息,提示颜色不包含 FromHex 的定义。
  • 您正在跨平台工作,或者您只是针对 ios 或 android 或其他东西?
  • 只针对安卓。我没有使用 Xamarin 表单
  • 如果您不使用表单,您可能需要查看stackoverflow.com/questions/5248583/…
【解决方案2】:

我相信您正在寻找接收字符串并返回整数颜色的 ParseColor 方法。

view.BackgroundColor = Android.Graphics.Color.ParseColor("#FF6A00");

支持的格式有:

  • #RRGGBB
  • #AARRGGBB
  • “红色”、“蓝色”、“绿色”、“黑色”、“白色”、“灰色”、“青色”、“洋红色”、“黄色”、“浅灰色”、“深灰色”

【讨论】:

    【解决方案3】:

    要以编程方式更改 .xaml 文件中某些文本的颜色,您可以这样做,例如切换循环:

    FindViewById<TextView>(Resource.Id.->insert the id/name from the .xaml file <-).SetBackgroundColor(Color.Red);
    break;
    

    希望对你有帮助

    【讨论】:

      【解决方案4】:

      为我工作:

       BackgroundColor = Color.FromHex("#fcf2e4");
      

      我的 Xamarin.Forms 版本:5.0.0.2012。

      我找到了代码Here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-06-06
        • 1970-01-01
        • 2019-03-26
        • 2017-06-25
        • 2016-04-02
        • 1970-01-01
        • 2019-07-20
        相关资源
        最近更新 更多