Android 按钮不透明度/透明度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20643999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:12:44  来源:igfitidea点击:

Button opacity/transparency

androidxmlbuttontransparencyopacity

提问by user3104504

main.xml:

主文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     android:background="@drawable/background"
        android:gravity="center"
        android:color="#66FF0000"

    >


    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/noua"
        android:textStyle="bold"
        android:textColor="#808080"
         />


    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/zece"
        android:textStyle="bold"
        android:textColor="#808080" />

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/unspe"
        android:textStyle="bold"
        android:textColor="#808080" />

    <Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/doispe"
        android:textStyle="bold"
        android:textColor="#808080"
/>

</LinearLayout>

This is my main.xml, I am trying to make the buttons the most transparent, but I want still to see them and I don't know what to add and where to add, please correct my xml with the low opacity on bottons. Premeditated thank you:D

这是我的 main.xml,我试图使按钮最透明,但我仍然想看到它们,但我不知道要添加什么以及在哪里添加,请使用按钮上的低不透明度更正我的 xml。有预谋的谢谢:D

回答by CSmith

check How to Set Opacity (Alpha) for View in Android

检查如何在 Android 中为视图设置不透明度(Alpha)

You can set transparency on background of objects, use #XX808080, where XX is the alpha/transparency value.

您可以设置对象背景的透明度,使用 #XX808080,其中 XX 是 alpha/透明度值。

android:background="#80FF0000"

this will be a half-transparent red background.

这将是一个半透明的红色背景。

You can set transparency on the button text using the textColor property in the same manner:

您可以使用 textColor 属性以相同的方式设置按钮文本的透明度:

android:textColor="#80FF0000"

You can also achieve through code in an animation

也可以通过动画中的代码实现

AlphaAnimation alphaAnim = new AlphaAnimation(1.0f, 0.5f);
alphaAnim.setDuration (400);
myButton.getBackground().startAnimation(alphaAnim);  // set alpha on background

or directly:

或直接:

myButton.getBackground().setAlpha(0.5f);   // added in API 11, sets alpha on background

both methods set the alpha to 50%

两种方法都将 alpha 设置为 50%

Lastly, you can try adding android:alpha for your XML:

最后,您可以尝试为您的 XML 添加 android:alpha :

<Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/zece"
        android:textStyle="bold"
        android:textColor="#808080" 
android:alpha="0.5"
/>

回答by yahya

You probably cant change its opacity with code, because they are 9patches. You need to create your own 9patch and set it to buttons as background. If you want a solid color, of course you can also use something like:

您可能无法使用代码更改其不透明度,因为它们是 9patches。您需要创建自己的 9patch 并将其设置为按钮作为背景。如果你想要纯色,当然你也可以使用类似的东西:

android:background="#80363636"

Here #AARRGGBB, how low you keep first two digits you get that opacity

在这里#AARRGGBB,你保持前两位数字的程度有多低,你会得到那个不透明度

回答by dangVarmit

in code, you can get an instance of the button and set the background drawable's alpha manually.

在代码中,您可以获取按钮的实例并手动设置背景可绘制对象的 alpha。

 Button btn = (Button) findViewById(..);
 btn.getBackground().setAlpha( a );  // where a : 0..255 : 0=transparent, 255=fully opaque