【问题标题】:Background color using rgb on android (XML preferred)?在android上使用rgb的背景颜色(首选XML)?
【发布时间】:2016-11-09 04:59:15
【问题描述】:

这很傻,但是如何在android上放一个背景rgb?

我正在尝试放置 RGB 颜色的背景,但我找不到怎么做。

android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background= ?
android:orientation="vertical" >

我已经这样做了,但不起作用

Color.rgb(200,0,0)

【问题讨论】:

  • 使用 #ff0000 获得红色
  • 问题是我需要用RGB格式@NabinKhadka
  • 为什么你需要在RGB中做任何强有力的理由? @Carlos Uriel Santiago
  • 学校作业 :( @Kinjal
  • 你不能在 xml(布局)中使用 RGB 颜色,你必须像 anyView.setBackgroundColor(Color.rgb(200, 00, 00)); 这样以编程方式完成

标签: android xml android-layout colors


【解决方案1】:

您可以在Android中使用十六进制代码设置RGB颜色,例如十六进制代码#RRGGBB

以下是一些示例颜色:

Color   Hex RGB  Decimal Code
White   #FFFFFF (255,255,255)
Red     #FF0000 (255,0,0)
Lime    #00FF00 (0,255,0)
Blue    #0000FF (0,0,255)

例如,您想在视图背景中设置蓝色

android:background= "#0000FF"

最好在Color res 文件夹中定义您的颜色

【讨论】:

    【解决方案2】:

    不确定是否可以使用 XML 中的 RGB 值。但我很确定你可以从 Java 中做到这一点。创建组件的实例并使用方法setBackgroundColor()。如下使用:

    yourComponent.setBackgroundColor(Color.rgb(200, 00, 00));
    

    【讨论】:

      【解决方案3】:

      应该是

      View.setBackgroundColor(rgb(255,0,0))
      

      你可能会得到一个错误导入

      import android.graphics.Color.rgb
      

      【讨论】:

        猜你喜欢
        • 2013-04-27
        • 2011-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-05
        相关资源
        最近更新 更多