【问题标题】:Android: Dynamically Change TextView Background colorAndroid:动态更改 TextView 背景颜色
【发布时间】:2011-12-26 18:55:49
【问题描述】:

我的活动中有以下文本视图。我想动态更改文本视图的背景颜色。

我的问题是我不想从 Resouce 文件或其他 colors.RED 方法中获取颜色。我在网络安全模式下从 webservie 获取颜色(即#FFF、#000 等)。

如何将这些颜色作为背景传递给 TextView。提前感谢您的宝贵时间。

<TextView
                android:id="@+id/colorCode"
                android:layout_width="40dp"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true" android:background="#FF0000" android:layout_marginRight="5dp"/>

【问题讨论】:

    标签: android textview background-color


    【解决方案1】:

    您现在可以通过编程方式更改背景颜色。 100% 为我工作。试试看。

     RelativeLayout relativeLayout = findViewById(R.id.relativeLayout);
     relativeLayout.setBackgroundColor(ContextCompat.getColor(yourContext, R.color.yourColor));
    

    稍后谢谢我。

    【讨论】:

      【解决方案2】:

      你可以像这样从 android 设置颜色或以 rbg 格式设置颜色:

      TextView txtView = (TextView) findViewById(R.id.yourId);
      txtView.setBackgroundColor(Color.parseColor("#AA3456"));
      

      或:

      txtView.setBackgroundColor(Color.BLUE);
      

      【讨论】:

        【解决方案3】:

        保存在 res/values/colors.xml 的 XML 文件:

        <?xml version="1.0" encoding="utf-8"?>
        <resources>
           <color name="opaque_red">#f00</color>
           <color name="translucent_red">#80ff0000</color>
        </resources>
        

        然后从您的程序中访问这些颜色,如下所示:

        Resources res = getResources();
        int color = res.getColor(R.color.opaque_red);
        textView.setBackgroundColor(color);
        

        【讨论】:

          【解决方案4】:

          下面是 sn-p 可能会帮助你 txtChannelNameTextView 的对象

           txtChannelName.setBackgroundColor(Color.RED);
          

          txtChannelName.setBackgroundColor(Color.parseColor("#ffffff"));
          

          【讨论】:

          • 谢谢,但我已经知道这种方法了。我在我的问题中也提到过这个 //来自 Resouce 文件的颜色或其他颜色。RED 方法//我想知道我是否可以将#FFF 作为输入传递给 textview 的背景颜色??。
          【解决方案5】:

          你可以试试:

          String color = "FF0000";   // For example your color is FF0000
          TextView txt = new TextView(this);         
          txt.setBackgroundColor(Integer.parseInt(color, 16)+0xFF000000);
          

          //This is the most preferrable
          txt.setBackgroundColor(Color.parseColor("#FF0000"));    
          

          【讨论】:

            【解决方案6】:

            在您的活动中,您会执行以下操作:

            TextView textView = (TextView) findViewById(R.id.colorCode);
            int myDynamicColor = Color.parseColor("#FFFF00"); // Here you can pass a string taken from the user or from wherever you want.
            textView.setBackgroundColor(myDynamicColor);
            

            希望这会有所帮助。

            【讨论】:

            • 这就是我说的 :)
            猜你喜欢
            • 2015-12-17
            • 1970-01-01
            • 2013-09-27
            • 2014-04-05
            • 1970-01-01
            • 2022-11-18
            • 2014-06-06
            • 1970-01-01
            相关资源
            最近更新 更多