如何在背景中有颜色(即白色)的情况下,Textview
背景为约20%透明(不完全透明)?
答案 0 :(得分:1391)
使用以下代码为黑色:
<color name="black">#000000</color>
现在,如果我想使用不透明度,那么您可以使用以下代码:
<color name="black">#99000000</color> <!-- 99 is for alpha and others pairs zero's are for R G B -->
以下是不透明度代码:和all opacity level here
100% — FF
95% — F2
90% — E6
85% — D9
80% — CC
75% — BF
70% — B3
65% — A6
60% — 99
55% — 8C
50% — 80
45% — 73
40% — 66
35% — 59
30% — 4D
25% — 40
20% — 33
15% — 26
10% — 1A
5% — 0D
0% — 00
如果你总是忘记透明代码,那么你必须要看下面的链接,不用担心会忘记任何有关透明代码的内容: -
https://github.com/duggu-hcd/TransparentColorCode
textviewHeader.setTextColor(Color.parseColor(ColorTransparentUtils.transparentColor10(R.color.border_color)));
答案 1 :(得分:973)
使Alpha通道中的颜色为80%。例如,红色使用#CCFF0000
:
<TextView
...
android:background="#CCFF0000" />
在示例中,CC
是255 * 0.8 = 204
的十六进制数。请注意,前两个十六进制数字用于Alpha通道。格式为#AARRGGBB
,其中AA
为Alpha通道,RR
为红色通道,GG
为绿色通道,BB
为蓝色通道。
我假设20%透明意味着80%不透明。如果你的意思是另一种方式,而不是CC
使用33
这是255 * 0.2 = 51
的十六进制。
为了计算Alpha透明度值的正确值,您可以按照以下步骤操作:
100-20=80
)2^8=256
),表示范围从0到255. 255 * 0.8 = 204
。如果需要,舍入到最接近的整数。0xCC
。FF0000
,您将拥有CCFF0000
。答案 2 :(得分:122)
您可以管理颜色不透明度,更改颜色定义中的前2个字符:
# 99 000000
100% — FF
99% — FC
98% — FA
97% — F7
96% — F5
95% — F2
94% — F0
93% — ED
92% — EB
91% — E8
90% — E6
89% — E3
88% — E0
87% — DE
86% — DB
85% — D9
84% — D6
83% — D4
82% — D1
81% — CF
80% — CC
79% — C9
78% — C7
77% — C4
76% — C2
75% — BF
74% — BD
73% — BA
72% — B8
71% — B5
70% — B3
69% — B0
68% — AD
67% — AB
66% — A8
65% — A6
64% — A3
63% — A1
62% — 9E
61% — 9C
60% — 99
59% — 96
58% — 94
57% — 91
56% — 8F
55% — 8C
54% — 8A
53% — 87
52% — 85
51% — 82
50% — 80
49% — 7D
48% — 7A
47% — 78
46% — 75
45% — 73
44% — 70
43% — 6E
42% — 6B
41% — 69
40% — 66
39% — 63
38% — 61
37% — 5E
36% — 5C
35% — 59
34% — 57
33% — 54
32% — 52
31% — 4F
30% — 4D
29% — 4A
28% — 47
27% — 45
26% — 42
25% — 40
24% — 3D
23% — 3B
22% — 38
21% — 36
20% — 33
19% — 30
18% — 2E
17% — 2B
16% — 29
15% — 26
14% — 24
13% — 21
12% — 1F
11% — 1C
10% — 1A
9% — 17
8% — 14
7% — 12
6% — 0F
5% — 0D
4% — 0A
3% — 08
2% — 05
1% — 03
0% — 00
答案 3 :(得分:94)
使用带有#33------
等alpha值的颜色,并使用XML属性android:background=" "
将其设置为editText的背景。
255 * 0.2 = 51→十六进制33
答案 4 :(得分:79)
您可以尝试执行以下操作:
textView.getBackground().setAlpha(51);
在这里,您可以将不透明度设置为0(完全透明)到255(完全不透明)。 51正好是你想要的20%。
答案 5 :(得分:65)
答案 6 :(得分:27)
我采取了三种观点。在第一个视图中,我设置了完整(没有alpha)颜色,在第二个视图中我设置了一半(0.5 alpha)颜色,在第三个视图中我设置了浅色(0.2 alpha)。
您可以使用以下代码设置任何颜色并使用alpha获取颜色:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools = "http://schemas.android.com/tools"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:gravity = "center"
android:orientation = "vertical"
tools:context = "com.example.temp.MainActivity" >
<View
android:id = "@+id/fullColorView"
android:layout_width = "100dip"
android:layout_height = "100dip" />
<View
android:id = "@+id/halfalphaColorView"
android:layout_width = "100dip"
android:layout_height = "100dip"
android:layout_marginTop = "20dip" />
<View
android:id = "@+id/alphaColorView"
android:layout_width = "100dip"
android:layout_height = "100dip"
android:layout_marginTop = "20dip" />
</LinearLayout>
public class MainActivity extends Activity {
private View fullColorView, halfalphaColorView, alphaColorView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fullColorView = (View)findViewById(R.id.fullColorView);
halfalphaColorView = (View)findViewById(R.id.halfalphaColorView);
alphaColorView = (View)findViewById(R.id.alphaColorView);
fullColorView.setBackgroundColor(Color.BLUE);
halfalphaColorView.setBackgroundColor(getColorWithAlpha(Color.BLUE, 0.5f));
alphaColorView.setBackgroundColor(getColorWithAlpha(Color.BLUE, 0.2f));
}
private int getColorWithAlpha(int color, float ratio) {
int newColor = 0;
int alpha = Math.round(Color.alpha(color) * ratio);
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
newColor = Color.argb(alpha, r, g, b);
return newColor;
}
}
Kotlin版本:
private fun getColorWithAlpha(color: Int, ratio: Float): Int {
return Color.argb(Math.round(Color.alpha(color) * ratio), Color.red(color), Color.green(color), Color.blue(color))
}
完成强>
答案 7 :(得分:18)
所有十六进制值从100%到0%alpha,您可以使用下面提到的alpha值设置任何颜色。例如#FAFFFFFF(ARRGGBB)
100% — FF
99% — FC
98% — FA
97% — F7
96% — F5
95% — F2
94% — F0
93% — ED
92% — EB
91% — E8
90% — E6
89% — E3
88% — E0
87% — DE
86% — DB
85% — D9
84% — D6
83% — D4
82% — D1
81% — CF
80% — CC
79% — C9
78% — C7
77% — C4
76% — C2
75% — BF
74% — BD
73% — BA
72% — B8
71% — B5
70% — B3
69% — B0
68% — AD
67% — AB
66% — A8
65% — A6
64% — A3
63% — A1
62% — 9E
61% — 9C
60% — 99
59% — 96
58% — 94
57% — 91
56% — 8F
55% — 8C
54% — 8A
53% — 87
52% — 85
51% — 82
50% — 80
49% — 7D
48% — 7A
47% — 78
46% — 75
45% — 73
44% — 70
43% — 6E
42% — 6B
41% — 69
40% — 66
39% — 63
38% — 61
37% — 5E
36% — 5C
35% — 59
34% — 57
33% — 54
32% — 52
31% — 4F
30% — 4D
29% — 4A
28% — 47
27% — 45
26% — 42
25% — 40
24% — 3D
23% — 3B
22% — 38
21% — 36
20% — 33
19% — 30
18% — 2E
17% — 2B
16% — 29
15% — 26
14% — 24
13% — 21
12% — 1F
11% — 1C
10% — 1A
9% — 17
8% — 14
7% — 12
6% — 0F
5% — 0D
4% — 0A
3% — 08
2% — 05
1% — 03
0% — 00
答案 8 :(得分:16)
XML值alpha
采用双值。
由于API 11+
的范围是0f
到1f
(包括),0f
是透明的,1f
是不透明的:
android:alpha="0.0"
那是不可见的
android:alpha="0.5"
透视
android:alpha="1.0"
完全可见
这就是它的工作原理。
答案 9 :(得分:8)
我们也可以通过这种方式透明化。
白色代码-FFFFFF
70%白色-# B3 FFFFFF。
100%-FF, 95%-F2, 90%-E6, 85%-D9, 80%-CC, 75%-高炉, 70%-B3 , 65%-A6, 60%— 99, 55%— 8C, 50%-80, 45%— 73, 40%— 66, 35%— 59, 30%— 4D, 25%— 40, 20%— 33, 15%— 26, 10%-1A, 5%-0D, 0%— 00
答案 10 :(得分:6)
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alpha="0.9"
/>
Android API 11 +中的Alpha范围介于0(透明)和1(不透明)之间
答案 11 :(得分:3)
现在 Android Studio 3.3 和更高版本提供了一种内置功能,可以更改颜色的 Alpha 值
只需在Android Studio编辑器中单击一种颜色,然后在percentage
中提供 Alpha值。
有关更多信息,请参见下图
答案 12 :(得分:2)
使用此
查看textView下方的热门程度 android:alpha="0.38"
<强> XML 强>
android:color="#3983BE00" // Partially transparent sky blue
<强>动态强>
<强> btn.getBackground()setAlpha(128)。 // 50%透明
<强> tv_name.getBackground()setAlpha(128)。 // 50%透明
Where the INT ranges from 0 (fully transparent) to 255 (fully opaque).
<TextView
style="@style/TextAppearance.AppCompat.Caption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alpha="0.38"
android:gravity="start"
android:textStyle="bold"
tools:text="1994|EN" />
<强>机器人:阿尔法=&#34; 0.38&#34; 强>
Text View alpha property set 0.38 to your textView visibility is faid
答案 13 :(得分:2)
我知道,这是一个非常老的问题。
如果要使用颜色值,也可以将其简短版本与#ARGB
一起使用。其中A
是Alpha通道的值。
如果是白色,则具有以下透明度值:
#FFFF - 0%
#EFFF - 6,7%
#DFFF - 13,3%
#CFFF - 20,0%
#BFFF - 26,7%
#AFFF - 33,3%
#9FFF - 40,0%
#FFF8 - 46,7%
#7FFF - 53,3%
#6FFF - 60,0%
#5FFF - 66,7%
#4FFF - 73,3%
#3FFF - 80,0%
#2FFF - 86,7%
#1FFF - 93,3%
#0FFF - 100,0%
因此,您可以为TextView
添加以下行以获得20%的透明度:
<TextView
android:background="#CFFF"
... />
答案 14 :(得分:1)
如果您想使Kotlin的颜色透明50%,
X
答案 15 :(得分:0)
这是来自@Aromero的答案的程序化解决方案,用于计算Alpha通道的十六进制值。 :)
public static void main(String[] args) throws Exception {
final Scanner scanner = new Scanner(System.in);
int transPerc;
float fPerc;
System.out.println("Enter the transparency percentage without % symbol:");
while((transPerc=scanner.nextInt())>=0 && transPerc <=100){
fPerc = (float) transPerc / 100;
transPerc = Math.round(255 * fPerc);
System.out.println("= " + Integer.toHexString(transPerc));
System.out.print("another one please : ");
}
scanner.close();
}
答案 16 :(得分:0)
试用此代码:)
完全透明的十六进制代码 - &#34;#00000000&#34;
答案 17 :(得分:0)
在Kotlin中,您可以像这样使用alpha,
//Click on On.//
view.rel_on.setOnClickListener{
view.rel_off.alpha= 0.2F
view.rel_on.alpha= 1F
}
//Click on Off.//
view.rel_off.setOnClickListener {
view.rel_on.alpha= 0.2F
view.rel_off.alpha= 1F
}
希望这会对您有所帮助。谢谢
答案 18 :(得分:0)
我建议使用alpha
属性。
<TextView
android:alpha="0.8" />
或者现在您可以使用selector
。在background_color_25.xml
包中创建colors
。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="0.8" android:color="@color/background_color" />
</selector>
这是用法:
<TextView
android:background="@color/background_color_25" />