我想將一個顏色選擇器分配給一個LinearLayout的擴展類,所以我認爲它就像我們談論linearLayout一樣。LinearLayout上的選擇器顏色
我按照this後的說明,回答了關於形狀的說法。
現在我已經繪項目文件夾3 XML:
normal.xml文件
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#ffffffff" />
</shape>
pressed.xml文件
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#00000000" />
</shape>
最後,bg.xml文件
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/pressed" />
<item android:state_focused="true" android:drawable="@drawable/pressed" />
<item android:state_selected="true" android:drawable="@drawable/pressed" />
<item android:drawable="@drawable/normal" />
</selector>
我正在訪問這適用於以下方式:
Drawable d = getResources().getDrawable(context.getResources().getIdentifier("mypackageuri.tProject:drawable/bg", null, null));
view.setBackgroundDrawable(d);
「正常」狀態的細末,用顏色設置爲「normal.xml」,但沒有辦法跟其他的人,我按我的觀點,並沒有任何反應,這不是改變顏色以任何方式......
我不能看見我做錯了什麼......
謝謝
可能需要粘貼布局XML – azharb
哇這必須得到一個繪製資源的最迂迴的方式。 。代替 'getResources()getDrawable(context.getResources()則getIdentifier( 「mypackageuri.tProject:可拉伸/ BG」,NULL,NULL));' 嘗試 'getResources()getDrawable(R。 drawable.bg);'。除非我錯過了一些東西。 – dmon
@dmon好點,生病也試試!謝謝!! – Deitools