在android中设置按钮的透明度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20743124/
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:23:23  来源:igfitidea点击:

setting transparency to buttons in android

androidbutton

提问by irfan

I want to make Buttons with different transparency levels in android.I have used "@android:color/transparent". But it makes the button 100% transparent. I need a 70% transparent button. Here is the XML code that I am working on:

我想在 android.I 中制作具有不同透明度级别的按钮"@android:color/transparent"。但它使按钮 100% 透明。我需要一个 70% 透明的按钮。这是我正在处理的 XML 代码:

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:gravity="center" 
    android:layout_weight="1">

    <Button android:id="@+id/one" 
        android:text="@string/dtmf_1"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:layout_weight="1" 
        android:textColor="@color/white" ></Button>
    <Button android:id="@+id/two"  
        android:text="@string/dtmf_2"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:layout_weight="1"  
        android:textColor="@color/white" ></Button>
    <Button android:id="@+id/three" 
        android:text="@string/dtmf_3"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:layout_weight="1"  
        android:textColor="@color/white" ></Button>

</LinearLayout>

回答by Chintan Khetiya

Using XML

使用 XML

If you want to set color and along with that if you want to set transparent then you have to use that color code .

如果你想设置颜色,同时如果你想设置透明,那么你必须使用那个颜色代码。

android:color="#66FF0000"    // Partially transparent red
android:alpha="0.25"         // 25% transparent 

Using java

使用 java

And if you want to set dynamically (java code)then try this,

如果你想动态设置(java代码)然后试试这个,

myButton.getBackground().setAlpha(64);  // 25% transparent

- i.e .INT ranges from 0 (fully transparent) to 255 (fully opaque)

- 即 .INT 范围从 0(完全透明)到 255(完全不透明)

回答by Guillermo Merino

You can define your own "transparent" color in styles.xml and play with the alfa of the color, for example:

您可以在styles.xml 中定义自己的“透明”颜色并使用颜色的阿尔法,例如:

<color name="Transparent">#00000000</color>
<color name="Transparent80">#80000000</color>

EDIT: second one is 50% transparency

编辑:第二个是 50% 的透明度

回答by Gurfuffle

Try android:background="#70FF0000"in your button code. Works for me.

试试android:background="#70FF0000"你的按钮代码。为我工作。

回答by jacob

To set the background of button as transparent do:

要将按钮的背景设置为透明,请执行以下操作:

    android:background="@android:color/transparent"

回答by nikvs

You can set a background for the button,then achieve the transparency by adjusting the alpha attribute of the button,

您可以为按钮设置背景,然后通过调整按钮的 alpha 属性来实现透明度,

android:alpha="0.7"

Makes the opacity 70 percent.

使不透明度为 70%。

回答by basti

Use this code in your background color

在您的背景颜色中使用此代码

android:background="?android:attr/selectableItemBackground"

回答by Nambi

try adding this to your buttonandroid:color="#55000000""

尝试将此添加到您的按钮android:color="#55000000""

<Button android:id="@+id/three" android:text="@string/dtmf_3"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:layout_weight="1" 
        android:color="#55000000" <!--Here--!>
        android:textColor="@color/white" ></Button>

回答by gile

You can try setting

你可以试试设置

android:alpha="0.7"

property on your Button in xml layout file

xml 布局文件中 Button 上的属性

回答by Aditya Singh Rajput

Instead of this:

取而代之的是:

android:color="#66FF0000"    // Partially transparent red
android:alpha="0.25"         // 25% transparent 

you can use this:

你可以使用这个:

android:background="#00FFFFFF"

android:background="#00FFFFFF"

回答by Disha

Setting the android:alphaproperty for button will help... Set the colour you want and then set the alpha property.

设置android:alpha按钮的属性会有所帮助...设置您想要的颜色,然后设置 alpha 属性。

android:color="#e5e5e5"    
android:alpha="0.25"

0.25means 25%

0.25表示25%