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


当前回答

如果你想用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();
}

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

结果如屏幕截图所示。

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

我建议使用alpha属性。

<TextView
   android:alpha="0.8" />

或者现在你可以使用选择器。在colors包中创建background_color_25.xml。

<?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" />

使用一个带有alpha值的颜色,如#33------,并使用XML属性android:background=" "将其设置为editText的背景。

0%(透明)-> #00十六进制 20% -> #33 50% -> #80 75% -> # c0 100%(不透明)-> #FF

255 * 0.2 = 51→十六进制33