我如何使一个Textview的背景大约20%透明(不完全透明),在哪里有一个颜色的背景(即白色)?


当前回答

我知道,这是个很老的问题。

如果你想使用颜色值,你也可以使用#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"
    ... />

其他回答

查看下面的流行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" />

android:α= " 0.38 "

Text View alpha property set 0.38 to your textView visibility is faid 

在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
    }

结果如屏幕截图所示。

希望这对你有所帮助。谢谢

如果你想用kotlin制作50%透明的颜色,

val percentage = 50f/100 //50%
ColorUtils.setAlphaComponent(resources.getColor(R.color.whatEverColor), (percentage * 255).toInt())

下面是来自@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();
}

现在Android Studio 3.3和更高版本提供了一个内置的功能来改变颜色的Alpha值,

只需在Android工作室编辑器中单击一种颜色,并提供Alpha值的百分比。

更多信息见下图