How do I make the background of a Textview
about 20% transparent (not fully transparent), where there is a color in the background (i.e. white)?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
I have taken three Views. In the first view I set full (no alpha) color, on the second view I set half (0.5 alpha) color, and on the third view I set light color (0.2 alpha).
You can set any color and get color with alpha by using the below code:
File activity_main.xml
File MainActivity.java
Kotlin version:
Done
We can make transparent in this way also.
White color code - FFFFFF
70% white - #B3FFFFFF.
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
Make the color have 80% in the alpha channel. For example, for red use
#CCFF0000
:In the example,
CC
is the hexadecimal number for255 * 0.8 = 204
. Note that the first two hexadecimal digits are for the alpha channel. The format is#AARRGGBB
, whereAA
is the alpha channel,RR
is the red channel,GG
is the green channel andBB
is the blue channel.I'm assuming that 20% transparent means 80% opaque. If you meant the other way, instead of
CC
use33
which is the hexadecimal for255 * 0.2 = 51
.In order to calculate the proper value for an alpha transparency value you can follow this procedure:
100-20=80
)2^8=256
), meaning the range goes from 0 to 255.255 * 0.8 = 204
. Round to the nearest integer if needed.0xCC
.FF0000
, you will haveCCFF0000
.You can take a look at the Android documentation for colors.
You can try to do something like:
Here you can set the opacity between 0 (fully transparent) to 255 (completely opaque). The 51 is exactly the 20% you want.
Use a color with an alpha value like
#33------
, and set it as background of your editText using the XML attributeandroid:background=" "
.255 * 0.2 = 51 → in hex 33
In Android Studio there is a built-in tool to adjust the color and alpha/opacity value: