设置TextView控件的背景透明度和字体透明度

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

第1种:tv.setBackgroundColor(Color.argb(255, 0, 255, 0)); //背景透明度  

        tv.setTextColor(Color.argb(255, 0, 255, 0));   //文字透明度

第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透明度值

textview1.setTextColor(Color.argb(255, 0, 255, 0)); //文字透明度

最关键部分,设置字体透明度 argb(Alpha, R, G, B)

[java] view plaincopy
 
 1 package net.android.touming;
 2 
 3 import android.widget.TextView;
 4 import android.os.Bundle;
 5 import android.view.ViewGroup;
 6 import android.app.Activity;
 7 import android.graphics.Color;
 8 import android.widget.LinearLayout;
 9 
10 public class touming extends Activity {
11 
12  final int WRAP_CONTENT = ViewGroup.LayoutParams.WRAP_CONTENT;
13 
14  public void onCreate(Bundle icicle) {
15   super.onCreate(icicle);
16 
17   LinearLayout linearLayout = new LinearLayout(this);
18   linearLayout.setOrientation(LinearLayout.VERTICAL);
19   setContentView(linearLayout);
20 
21   TextView textview1 = new TextView(this);
22   textview1.setText("全部不透明=255");
23   //textview1.setBackgroundColor(Color.argb(255, 0, 255, 0)); //背景透明度
24   textview1.setTextColor(Color.argb(255, 0, 255, 0));   //文字透明度
25   linearLayout.addView(textview1, new LinearLayout.LayoutParams(WRAP_CONTENT,
26     WRAP_CONTENT));
27 
28   TextView textview2 = new TextView(this);
29   textview2.setText("部分透分155");
30   textview2.setBackgroundColor(Color.argb(155, 0, 255, 0));  //背景透明度
31   textview2.setTextColor(Color.argb(155, 0, 255, 0));  //文字透明度
32   linearLayout.addView(textview2, new LinearLayout.LayoutParams(WRAP_CONTENT,
33     WRAP_CONTENT));
34 
35   TextView textview3 = new TextView(this);
36   textview3.setText("部分透明55");
37   textview3.setBackgroundColor(Color.argb(55, 0, 255, 0));  ///背景透明度
38   textview3.setTextColor(Color.argb(55, 0, 255, 0));  //文字透明度
39   linearLayout.addView(textview3, new LinearLayout.LayoutParams(WRAP_CONTENT,
40     WRAP_CONTENT));
41 
42   TextView textview4 = new TextView(this);
43   textview4.setText("全部透明0");
44   //textview4.setBackgroundColor(Color.argb(0, 0, 255, 0)); //背景透明度
45   textview4.setTextColor(Color.argb(0, 0, 255, 0));  //文字透明度
46   linearLayout.addView(textview4, new LinearLayout.LayoutParams(WRAP_CONTENT,
47     WRAP_CONTENT));
48 
49  }
50 
51 }
原文地址:https://www.cnblogs.com/androidsj/p/3624211.html