Android:动态更改TextView背景颜色

Jay*_*ayu 9 android background-color textview

我在我的活动中有以下textview.我想动态更改textview的背景颜色.

我的问题是我不想从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"/>
Run Code Online (Sandbox Code Playgroud)

ing*_*abh 30

以下是片段可能会帮助你在哪里txtChannelName的对象TextView

 txtChannelName.setBackgroundColor(Color.RED);
Run Code Online (Sandbox Code Playgroud)

要么

txtChannelName.setBackgroundColor(Color.parseColor("#ffffff"));
Run Code Online (Sandbox Code Playgroud)


cha*_*aon 6

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

TextView txtView = (TextView) findViewById(R.id.yourId);
txtView.setBackgroundColor(Color.parseColor("#AA3456"));
Run Code Online (Sandbox Code Playgroud)

要么:

txtView.setBackgroundColor(Color.BLUE);
Run Code Online (Sandbox Code Playgroud)