按钮不透明度/透明度

时间:2013-12-17 20:27:50

标签: android xml button transparency opacity

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>

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

3 个答案:

答案 0 :(得分:15)

检查How to Set Opacity (Alpha) for View in Android

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

android:background="#80FF0000"

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

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

android:textColor="#80FF0000"

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

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

或直接:

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

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

最后,您可以尝试为您的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"
/>

答案 1 :(得分:1)

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

android:background="#80363636"

这里#AARRGGBB,你得到不透明度的前两位数有多低

答案 2 :(得分:0)

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

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