【问题标题】:Change color of a view dynamically depends on what color of background the view is in动态更改视图的颜色取决于视图的背景颜色
【发布时间】:2017-04-28 01:39:11
【问题描述】:

在 Android 中,有没有办法根据视图的背景类型动态更改视图的颜色?

例如,如果我在具有黑色背景的 ListView 上有一个带有白色文本颜色的 浮动 TextView,则文本颜色将清晰可见。但是,当我向下滚动时,如果 ListView 的下一部分具有白色背景,则 TextView 中的文本将不再可见,因为它是白底白字。

Android 中是否有任何方法可以更改此 TextView 的文本颜色,因为它以相似/相同的颜色到达背景?

【问题讨论】:

    标签: android listview colors


    【解决方案1】:

    嗯,这取决于背景的类型。

    如果您正在谈论具有颜色的视图(线性布局、表面视图等)中的背景,您必须获取文本视图在屏幕上的位置,并根据该点的背景颜色设置颜色.但这取决于颜色的类型。位图可以让你得到一个特定的像素:

    bmp.getPixel(x, y);
    

    然后你可以根据它来确定颜色。

    但是,使用 SurfaceView 变得更加困难,因为没有本地方法。

    此外,渐变填充的背景更难到达特定点。但如果您使用视图并设置了背景色,您可以:

    if (view.getBackground() == Color.RED){//View = the view with a background set using setBackground or android:background="@color or @drawable or whatever"
        tv.setTextColor(Color.WHITE); //then you adjsut the textview color
    }
    

    请注意,上面的示例仅在视图的背景定义为 android:background 或 setBackground 并且有颜色时才有效。使用位图时可以得到the pixel and the color based on that pixel


    【讨论】:

      【解决方案2】:

      不难的东西

      if view.getBackground() == .RED{
          yourStaff.text.setTextColor(Color.WHITE); 
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-06-11
        • 1970-01-01
        • 2010-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-16
        • 1970-01-01
        相关资源
        最近更新 更多