`
yuanlanjun
  • 浏览: 1186298 次
文章分类
社区版块
存档分类
最新评论

如何设置TexView控件的背景透明度和字体透明度

 
阅读更多

TextView tv = (TextView) findViewById(R.id.xx);

第1种:tv.setBackgroundColor(Color.argb(255, 0, 255, 0)); //背景透明度<wbr style="font-size:14px; line-height:21px; text-align:left"><span style="font-size:14px; line-height:21px; text-align:left"></span><wbr style="font-size:14px; line-height:21px; text-align:left"><span style="font-size:14px; line-height:21px; text-align:left"></span></wbr></wbr><wbr style="color:rgb(50,62,50); font-size:14px; line-height:21px; text-align:left"><br style="color:rgb(50,62,50); font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(50,62,50); font-size:14px; line-height:21px; text-align:left"></span><wbr style="color:rgb(50,62,50); font-size:14px; line-height:21px; text-align:left"><span style="font-size:14px; line-height:21px; text-align:left"><span style="color:#323e32"> </span><span style="color:#ff0000"><strong><span style="font-size:16px">tv</span>.setTextColor(Color.argb(255, 0, 255, 0));</strong></span></span><span style="color:#ff0000"><strong><wbr style="font-size:14px; line-height:21px; text-align:left"><span style="font-size:14px; line-height:21px; text-align:left"></span><wbr style="font-size:14px; line-height:21px; text-align:left"><span style="font-size:14px; line-height:21px; text-align:left">//文字透明度</span><wbr style="font-size:14px; line-height:21px; text-align:left"><span style="font-size:14px; line-height:21px; text-align:left"></span><wbr style="font-size:14px; line-height:21px; text-align:left"><span style="font-size:14px; line-height:21px; text-align:left"></span></wbr></wbr></wbr></wbr></strong></span></wbr></wbr>

第2种:tv.setTextColor(0xffff00ff);

0xffff00ff是int类型的数据,分组一下0x|ff|ff00ff,0x是代表颜色整数的标记,ff是表示透明度,ff00ff表示颜色,注意:这里ffff00ff必须是8个的颜色表示,不接受ff00ff这种6个的颜色表示。

颜色和不透明度 (alpha) 值以十六进制表示法表示。任何一种颜色的值范围都是 0 到 255(00 到 ff)。对于 alpha,00 表示完全透明,ff 表示完全不透明。表达式顺序是“aabbggrr”,其中“aa=alpha”(00 到 ff);“bb=blue”(00 到 ff);“gg=green”(00到ff);“rr=red”(00 到 ff)。例如,如果设置字体颜色的不透明度为 50% 的蓝色,则应指定以下值:7fff0000(如何把十进制的50换算成十六进制的50:十进制到其他进制用除,一直除到商为0,然后每次余数逆序排列就是结果,其他进制到十进制用乘,比如此处用到除法,80/16商5余0,再用前一次的商除16得商0余5,停止相除,逆序排列余数得到0x50)。

第3种:在xml文件中直接设置颜色值,同下。


Button或者ImageButton的背景设为透明或者半透明

xml文件

透明<Button android:background="#e0000000" ... />

透明<Button android:background="#00000000" ... />

Java代码

View v = findViewById(R.id.xx);//找到你要设透明背景的layout 的id

v.getBackground().setAlpha(100);//0~255透明度值





分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics