2013-12-17 60 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