2013-10-15 83 views
0

我有一個佈局,例如(我使用的片段):我該如何訪問drawable?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:minWidth="25px" 
    android:minHeight="25px"> 
<TextView 
    android:id="@+id/Text_Value" 
    android:text="0" 
    android:textSize="32dp" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/PlaySquareShape"/> 
</RelativeLayout> 

我有這樣的形狀,在上述佈局中的背景的TextView的:

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
     <solid android:color="#ffffff" /> 
     <stroke android:width="1dip" android:color="#4fa5d5"/> 
    </shape> 

我需要改變可繪製的顏色。我如何從代碼訪問它?

我基本上有很多顯示片段的實例,我需要訪問特定片段的背景drawable。

回答

3

你可以這樣做:

View v = findViewById(R.id.Text_Value); 
ShapeDrawable d = (ShapeDrawable) v.getBackground(); 

或者,如果你有一個特定片段的引用:

View v = frag.getView().findViewById(R.id.Text_Value); 
+0

完美!我會試試看。謝謝:)我仍然是新的。 – Maximus

+0

它的工作!唯一的區別是我的背景是GradientDrawable :) – Maximus

相關問題