Taw*_*ani 159 android textview
以编程方式设置android的背景颜色TextView似乎不起作用.我错过了什么!
TextView et = new TextView(activity);
et.setText("350");
et.setBackgroundColor(R.color.white);
Run Code Online (Sandbox Code Playgroud)
我的res/values文件夹中也有这个文件(colors.xml)
<resources>
<color name="white">#ffffffff</color>
<color name="black">#ff000000</color>
</resources>
Run Code Online (Sandbox Code Playgroud)
[编辑]:此外,设置文本颜色会导致TextView消失.
TextView c1 = new TextView(activity);
c1.setTextColor(R.color.solid_red);
c1.setText("My Text");
Run Code Online (Sandbox Code Playgroud)
bha*_*982 317
使用 et.setBackgroundResource(R.color.white);
Seb*_*CHA 72
试试这个:
TextView c1 = new TextView(activity);
c1.setTextColor(getResources().getColor(R.color.solid_red));
c1.setText("My Text");
Run Code Online (Sandbox Code Playgroud)
我同意颜色和资源具有相同的类型,但我也花了几个小时来找到这个解决方案.
Dmi*_*try 65
设置红色:
textView.setBackgroundColor(0xfff00000);
Run Code Online (Sandbox Code Playgroud)
要么
<color name="solid_red">#fff00000</color>
textView.setBackgroundResource(R.color.solid_red);
Run Code Online (Sandbox Code Playgroud)
bad*_*key 20
我有一个类似的问题,我在创建一个数字颜色而不考虑领先的alpha通道.即. mytext.setTextColor(0xFF0000)(以为这会是红色的).虽然这是一种红色,但它也是100%透明的,因为it = 0x00FF0000; 正确的100%不透明值是0xFFFF0000或mytext.setTextcolor(0xFFFF0000).
只有这一行代码以编程方式更改了背景
tv.setBackgroundColor(Color.parseColor("#808080"));
Run Code Online (Sandbox Code Playgroud)
小智 8
好吧,我有网络服务返回十六进制格式的颜色,如"#CC2233",我想通过使用setBackGroundColor()将此颜色放在textView上,所以我使用android Color类获取十六进制字符串的int值并将其传递给提到的功能.一切正常.这是一个例子:
String myHexColor = "#CC2233";
TextView myView = (TextView) findViewById(R.id.myTextView);
myView.setBackGroundColor(Color.pasrsehexString(myHexColor));
Run Code Online (Sandbox Code Playgroud)
PS发布了这个答案,因为其他解决方案对我不起作用.我希望这会帮助别人:)
这里有一点细节,
如果你在活动中使用这个
textview.setBackground(ContextCompat.getColor(this,R.color.yourcolor));
Run Code Online (Sandbox Code Playgroud)
如果你在片段中使用下面的代码
textview.setBackground(ContextCompat.getColor(getActivity(),R.color.yourcolor));
Run Code Online (Sandbox Code Playgroud)
如果您在 recyclerview 适配器中,请使用以下代码
textview.setBackground(ContextCompat.getColor(context,R.color.yourcolor));
// use holder.textview if you are in onBindviewholder
//here context is passed from fragment
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
242080 次 |
| 最近记录: |