2013-04-21 111 views
0

我已閱讀了很多關於以編程方式更改drawable顏色的問題,但它們似乎與實際佈局無關。當我試試這個:以@drawable xml編程方式更改佈局顏色

RelativeLayout firstWord = (RelativeLayout)getActivity().findViewById(R.id.topBG); 
Drawable layoutBG = firstWord.getDrawable(); 
firstWord = buttonBackground.mutate(); 
firstWord.setColorFilter(0xFFFF0000,Mode.MULTIPLY); 

我得到這個錯誤:

The method getDrawable() is undefined for the type RelativeLayout

我用繪製給我的佈局,圓潤的邊角,但應用程序的一部分是,在每一次點擊的背景顏色變化,因此我需要知道如何更改代碼中的顏色。

前移動到繪製,我用的是:

這裏是我的繪製:

round_corners.xml

<shape 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <stroke 
     android:width="1dp" 
     android:color="#000000" /> 

    <padding 
     android:left="1dp" 
     android:top="1dp" 
     android:right="1dp" 
     android:bottom="1dp" /> 

    <corners 
     android:radius="25dp" /> 

</shape> 

這是我的佈局XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".VulgarActivity" 
    android:id="@+id/bottomBG" 
    android:background="#ffffff" 
    > 
    <View 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="@drawable/drop_shadow" 
     > 
    </View> 
    <RelativeLayout 
     android:id="@+id/topBG" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginRight="10dp" 
     android:layout_marginBottom="10dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="10dp" 
     android:background="@drawable/round_corners" 
     > 
    <TextView 
     android:id="@+id/leftWord" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:text="Push" 
     android:textColor="#ffffff" 
     android:textSize="50sp" 
     android:shadowColor="#080808" 
     android:shadowRadius="10.0" 
     android:shadowDx="5" 
     android:shadowDy="5" 
     /> 
    </RelativeLayout> 

</RelativeLayout> 

謝謝提前任何和所有幫助:)

只是一個想法:

我可以在「round_corners.xml」顏色值更改爲@color引用(例如:

android:color="@color/switcher" 

,並把這在「顏色」文件夾中的值

<color name="switcher">#000000</color> 

以編程方式更改顏色會更容易嗎?

+0

可以顯示爲的RelativeLayout的XML? – j2emanue 2013-04-21 01:06:51

+0

@ j2emanue完成:) – Psest328 2013-04-21 01:09:45

+0

好吧。我會看看。 – j2emanue 2013-04-21 01:11:55

回答

0

您可以使用這些行,而不是,即時得到背景繪製第二行:

RelativeLayout firstWord = (RelativeLayout)getActivity().findViewById(R.id.topBG); 
Drawable layoutBG = firstWord.getBackground(); 
buttonBackground = buttonBackground.mutate(); 
buttonBackground.setColorFilter(0xFFFF0000,Mode.MULTIPLY); 
+0

廢話,沒有注意到我打錯了。它不是'buttonbackground',它仍然是'firstWord'。現在編輯我的OP。現在將嘗試修正,雖然 – Psest328 2013-04-21 01:25:06

+0

行,所以它接受以下代碼,但它將背景更改爲黑色,然後沒有別的: 可繪製layoutBG = firstWord.getBackground(); \t \t \t \t layoutBG.setColorFilter(color,Mode.MULTIPLY); – Psest328 2013-04-21 01:35:43

相關問題