2011-10-12 69 views
3

在繪製文件夾:可用選擇器繪製?

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" 
      android:drawable="@drawable/icon2" /> 
    <item android:drawable="@drawable/icon3" /> 
</selector> 

和佈局僅僅是簡單的線性佈局填滿了整個空間

<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/on_touch_" 
    android:weightSum="1" android:focusable="true" android:focusableInTouchMode="true"> 
</LinearLayout> 

,當我按下這個什麼都不會發生

如果我添加,例如,一些文本視圖並分配android:background="@drawable/on_touch_",然後按下textview正確更改圖像。

線性佈局的問題在哪裏,爲什麼按下時不會改變圖像?

編輯: 我相信我的可繪製選擇器是好的,因爲我把它作爲背景對其他元素,它工作。

但我的問題是如何設置繪製到元素

+0

你想要實現什麼? –

回答

0

嘗試

save_selector.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector 
    xmlns:android="http://schemas.android.com/apk/res/android"> 



    <item android:state_focused="true" android:state_pressed="false" 
     android:drawable="@drawable/save_hover_new" /> 
    <item android:state_focused="true" android:state_pressed="true" 
     android:drawable="@drawable/save_hover_new" /> 
    <item android:state_focused="false" android:state_pressed="true" 
     android:drawable="@drawable/save_hover_new" /> 

    <item android:drawable="@drawable/save_new" /> 

</selector> 

,並給佈局的像 機器人選擇背景:背景=「@ drawable/save_selector

+0

tnx,但問題不在選擇器中。我把同樣的選擇器drawable到其他組件,如textview和它的作品,但它不與佈局 – Lukap

+0

我不認爲選擇器可以適用於佈局... – Richa

+0

是的,絕對可以做到!我已經做到了,但不是在根佈局上,我的問題是我無法將其添加到根佈局 – Lukap

0

我會確保我沒有繪畫能夠同名。您似乎正在使用on_touch_.xml作爲您的選擇器。也許還有on_touch_.png

我還會確保我不會在代碼中將背景設置爲其他內容或使其不可點擊。

+0

不,我沒有on_touch_.png。 – Lukap

0

我在一個工作正常的根元素中有一個選擇器;按下時背景會切換。它不是活動佈局的根元素,但它是XML文件的根元素。

背景是在第一分配有PNG:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:background="@drawable/panel" 
> 
    ... 

我分配背景到分配一個OnClickListener僅當選擇器:

private void setClickListener(OnClickListener ocl) { 

    View boxRoot = findViewById(R.id.box_root); 
    if (ocl != null) { 
     boxRoot.setOnClickListener(ocl); 
     boxRoot.setBackgroundDrawable(getResources().getDrawable(R.drawable.panel_arrow_right_bgstate)); 

     setClickable(true); 
        ... 

在我的XML中,我使用android:clickable="true"但隨後我還添加了android:focusable="true" android:focusableInTouchMode="true"以符合您的情況。這也工作得很好,背景切換爲我所有的四個州。

// panel_arrow_right_bgstate.xml 
<?xml version="1.0" encoding="utf-8"?> 

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/panel_arrow_right_normal"/> 
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/panel_arrow_right_pressed"/> 
<item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/panel_arrow_right_selected"/> 
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/panel_arrow_right_pressed"/> 

如果這不是爲你工作,別的東西是你的代碼錯誤,別的地方。

當我添加或刪除drawables時,Eclipse有時變得腥並且混合起來。我的正常測量:

  1. 清潔工程
  2. 刷新資源在Eclipse文件夾
  3. 刪除根\ com.comp.proj \ R.java
6

添加到您的佈局:

android:clickable="true" 

這將設置按下狀態,當你點擊它。