2013-12-17 62 views
2

的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

回答

10

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

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

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:

<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" 
/> 
+0

你可以给我发送我的xml编辑请:|? – user3104504

+0

编辑答案,请参阅android:alpha属性 – CSmith

+0

非常感谢你,这是我需要的! – user3104504

0

你可能不能用代码来改变它的不透明性,因为它们是9个patch。您需要创建自己的9patch并将其设置为按钮作为背景。如果你想有一个坚实的颜色,当然你也可以使用类似:

android:background="#80363636" 

这里#AARRGGBB,你如何保持低前两位你得到的不透明度

+0

可能你送我的我的XML编辑请:|? – user3104504

0
代码

,你可以得到一个实例并手动设置背景可绘制的alpha。

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

你可以给我发送我的XML编辑请:|? – user3104504

+0

我不认为你可以在xml文件中设置alpha。没有任何可绘制的资源允许它[见http://developer.android.com/guide/topics/resources/drawable-resource.html]。你必须在代码中设置alpha或者使你自己的半透明背景可绘制并使用 – dangVarmit